当意指PyEval_InitThreads被称为? [英] When is PyEval_InitThreads meant to be called?
问题描述
我有点困惑时,我应该叫 PyEval_InitThreads
。一般情况下,我的理解是 PyEval_InitThreads只要使用非Python的线程(即这是一个扩展模块内催生一个线程)
必须被调用。
I'm a bit confused about when I'm supposed to call PyEval_InitThreads
. In general, I understand that PyEval_InitThreads
must be called whenever a non-Python thread (i.e. a thread that is spawned within an extension module) is used.
不过,我很困惑,如果 PyEval_InitThreads
是其中嵌入了Python间preTER或Python方案,其中进口C-扩展模块,或两者的C程序
However, I'm confused if PyEval_InitThreads
is for C programs which embed the Python interpreter, or Python programs which import C-extension modules, or both.
所以,如果我写一个C扩展模块,将在内部启动一个线程,做我需要调用 PyEval_InitThreads
初始化模块时?
So, if I write a C extension module that will internally launch a thread, do I need to call PyEval_InitThreads
when initializing the module?
此外, PyEval_InitThreads
隐式获取全球国米preTER锁 。因此调用 PyEval_InitThreads后
,presumably的GIL必须的发布的僵局或将接踵而至。那么,你如何释放锁?阅读文档后,<一个href=\"http://docs.python.org/dev/c-api/init.html#PyEval_ReleaseLock\"><$c$c>PyEval_ReleaseLock()$c$c>似乎是释放GIL的方式。然而,在实践中,如果我用下面的code A C扩展模块:
Also, PyEval_InitThreads
implicitly acquires the Global Interpreter Lock. So after calling PyEval_InitThreads
, presumably the GIL must be released or deadlock will ensue. So how do you release the lock? After reading the documentation, PyEval_ReleaseLock()
appears to be the way to release the GIL. However, in practice, if I use the following code in a C extension module:
PyEval_InitThreads();
PyEval_ReleaseLock();
...然后在运行时的Python中止与
...then at runtime Python aborts with:
Fatal Python error: drop_gil: GIL is not locked
那么,你如何与 PyEval_InitThreads
?
推荐答案
大多数应用程序永远需要了解 PyEval_InitThreads()
的。
Most applications never need to know about PyEval_InitThreads()
at all.
你应该使用它,如果你的嵌入应用程序或扩展模块将被从多个线程的,它催生了本身的Python的外使Python的C API调用的唯一一次。
The only time you should use it is if your embedding application or extension module will be making Python C API calls from more than one thread that it spawned itself outside of Python.
不要在以后将使得Python的C API调用的任何线程中调用 PyEval_ReleaseLock()
(之前那些除非你重新获得它)。在这种情况下,你应该使用 Py_BEGIN_ALLOW_THREADS
和 Py_END_ALLOW_THREADS
宏来代替。
Don't call PyEval_ReleaseLock()
in any thread which will later be making Python C API calls (unless you re-acquire it before those). In that case you should really use the Py_BEGIN_ALLOW_THREADS
and Py_END_ALLOW_THREADS
macros instead.
这篇关于当意指PyEval_InitThreads被称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!