Python GIL:是否django save()阻止? [英] Python GIL: is django save() blocking?

查看:149
本文介绍了Python GIL:是否django save()阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的django应用程序将django模型保存到远程数据库。有时候,这些保存是突发的。为了将应用程序的主线程(* thread_A *)从保存多个对象的时间到数据库,我想到使用 collections.deque 并拥有* thread_B *保存它们。

My django app saves django models to a remote database. Sometimes the saves are bursty. In order to free the main thread (*thread_A*) of the application from the time toll of saving multiple objects to the database, I thought of transferring the model objects to a separate thread (*thread_B*) using collections.deque and have *thread_B* save them sequentially.

但我不确定这个方案。 save()返回新数据库条目的ID,所以它只在数据库响应后才结束,这是在事务结束时。

Yet I'm unsure regarding this scheme. save() returns the id of the new database entry, so it "ends" only after the database responds, which is at the end of the transaction.

django.db.models.Model.save()真的阻止 GIL ,并在交易期间发布其他python线程

Does django.db.models.Model.save() really block GIL-wise and release other python threads during the transaction?

推荐答案

Django的 save()对GIL没有任何特殊的影响。事实上,Python代码中几乎没有什么可以用GIL来执行,当它被执行时,线程必须保持GIL。

Django's save() does nothing special to the GIL. In fact, there is hardly anything you can do with the GIL in Python code -- when it is executed, the thread must hold the GIL.

只有两种方式GIL可以在 save()中发布:

There are only two ways the GIL could get released in save():

第二点可能是您要查找的内容 - 执行SQL COMMIT ,在执行期间,SQL后端会释放GIL。然而,这取决于SQL界面,我不知道流行的是否真的释放了GIL *。

The second point could be what you are looking for -- a SQL COMMITis executed and during that execution, the SQL backend releases the GIL. However, this depends on the SQL interface, and I'm not sure if the popular ones actually release the GIL*.

此外, save )做的不只是运行几个更新/插入语句和一个 COMMIT ;它在Python中做了大量的簿记,它必须持有GIL。总而言之,我不确定你会从 save()到另一个线程中获得任何东西。

Moreover, save() does a lot more than just running a few UPDATE/INSERT statements and a COMMIT; it does a lot of bookkeeping in Python, where it has to hold the GIL. In summary, I'm not sure that you will gain anything from moving save() to a different thread.

更新:从源代码中,我了解到$ code> sqlite 模块和 psycopg 在调用数据库例程时,会释放GIL,我猜其他接口都是一样的。

UPDATE: From looking at the sources, I learned that both the sqlite module and psycopg do release the GIL when they are calling database routines, and I guess that other interfaces do the same.

这篇关于Python GIL:是否django save()阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆