Django的“模型”API线程安全吗?

时间:2022-04-23 11:47:15

Specifically, I'm only talking about modifying separate instances of a Model (not sharing the same exact instance) across threads. But is it safe to be calling save() from one thread, while multiple other threads are invoking Model.objects.query() or Model.objects.get() for example?

具体来说,我只是谈论跨线程修改模型的单独实例(不共享相同的实例)。但是从一个线程调用save()是否安全,而多个其他线程正在调用Model.objects.query()或Model.objects.get()?

1 个解决方案

#1


1  

As far as we speak about save versus get and query, you are safe, since distinct querysets objects are involved. In fact, every query, filter, get call and so on creates a new queryset instance, and do not modify any previously existed object.

就我们所说的保存与获取和查询而言,您是安全的,因为涉及不同的查询集对象。实际上,每个查询,过滤器,调用等都会创建一个新的查询集实例,而不会修改任何以前存在的对象。

But obviously, you can run into issues when accessing/modifying same db record simultaneously from several threads/clients so on.

但显然,当您从多个线程/客户端同时访问/修改相同的数据库记录时,您可能会遇到问题。

As I remember, to deal with database updates consistency, in oracle db and mysql inndodb with disabled autocommit there is a select for update statement.

我记得,为了处理数据库更新的一致性,在oracle db和带有禁用自动提交的mysql inndodb中有一个select for update语句。

#1


1  

As far as we speak about save versus get and query, you are safe, since distinct querysets objects are involved. In fact, every query, filter, get call and so on creates a new queryset instance, and do not modify any previously existed object.

就我们所说的保存与获取和查询而言,您是安全的,因为涉及不同的查询集对象。实际上,每个查询,过滤器,调用等都会创建一个新的查询集实例,而不会修改任何以前存在的对象。

But obviously, you can run into issues when accessing/modifying same db record simultaneously from several threads/clients so on.

但显然,当您从多个线程/客户端同时访问/修改相同的数据库记录时,您可能会遇到问题。

As I remember, to deal with database updates consistency, in oracle db and mysql inndodb with disabled autocommit there is a select for update statement.

我记得,为了处理数据库更新的一致性,在oracle db和带有禁用自动提交的mysql inndodb中有一个select for update语句。