问题描述
其中一个Python的最强点是易用性写作C和C ++的扩展,加快code处理器密集的地方。这些扩展可避免全球国米preTER锁还是他们也是由GIL的限制?如果没有,那么这个易于扩展更是一个杀手级的功能比我previously实现。我怀疑答案不是简单的是或否,但我不知道,所以我在这里问这个问题在计算器上。
One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of extension" is even more of a killer feature than I previously realized. I suspect the answer is not a simple yes-or-no but I am not sure, so I am asking the question here on StackOverflow.
推荐答案
是的,调用C扩展(通过Python调用的C例程)仍然受GIL。
Yes, calls to C extensions (C routines called from Python) are still subject to the GIL.
不过,你可以的手动的释放GIL的C扩展里面,只要你小心控制返回给Python的VM之前再宣称它。
However, you can manually release the GIL inside your C extension, so long as you are careful to re-assert it before returning control to the Python VM.
有关信息,看一看在 Py_BEGIN_ALLOW_THREADS
和 Py_END_ALLOW_THREADS
宏:<一href=\"http://docs.python.org/c-api/init.html#thread-state-and-the-global-inter$p$pter-lock\">http://docs.python.org/c-api/init.html#thread-state-and-the-global-inter$p$pter-lock
For information, take a look at the Py_BEGIN_ALLOW_THREADS
and Py_END_ALLOW_THREADS
macros: http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock
这篇关于并发:是用C / C Python扩展++受全球国米preTER锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!