发布于 2015-08-30 08:01:29 | 114 次阅读 | 评论: 0 | 来源: 网络整理

问题

You have C extension code in that you want to execute concurrently with other threads in the Python interpreter. To do this, you need to release and reacquire the global in‐ terpreter lock (GIL).


解决方案

In C extension code, the GIL can be released and reacquired by inserting the following macros in the code:

#include “Python.h” ...

PyObject *pyfunc(PyObject *self, PyObject *args) {
... Py_BEGIN_ALLOW_THREADS // Threaded C code. Must not use Python API functions ... Py_END_ALLOW_THREADS ... return result;

}


讨论

The GIL can only safely be released if you can guarantee that no Python C API functions will be executed in the C code. Typical examples where the GIL might be released are in computationally intensive code that performs calculations on C arrays (e.g., in ex‐ tensions such as numpy) or in code where blocking I/O operations are going to be per‐ formed (e.g., reading or writing on a file descriptor). While the GIL is released, other Python threads are allowed to execute in the interpreter. The Py_END_ALLOW_THREADS macro blocks execution until the calling threads reacquires the GIL in the interpreter.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务