Gil Operation
For the C extension code to execute correctly with other processes in the Python interpreter, you need to release and retrieve the Global Interpreter lock (GIL).
In the Python interface wrapper to release and regain the global Interpreter lock (GIL), this program loses the GIL run, and other threads can run without ignoring this function until Py_end_allow_threads:
#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;}
You can safely release the Gil only if you make sure that no Python C API functions are executed in C. A common scenario in which the Gil needs to be freed is in computationally intensive code where calculations are performed on the C array (for example, in NumPy) or when a blocking I/O operation is performed (such as when reading or writing on a file descriptor).
When the Gil is freed, other Python threads are allowed to execute in the interpreter. Py_END_ALLOW_THREADS
the macro blocks execution until the calling thread acquires the Gil again.
Mixing threads in C and Python
mixed with C, Python, and threads, some threads are created in C, beyond the control of the Python interpreter, and some threads also use the functions in the Python C API to ensure proper initialization and management of the Python Global Interpreter Lock (GIL).
To do this, you can put the following code in your C code and make sure it is called before any threads are created:
#include <Python.h> ... if (! Pyeval_threadsinitialized ()) { pyeval_initthreads (); } ...
For any C code that calls the Python object or the Python C API, make sure you have correctly fetched and freed the Gil, which can be used PyGILState_Ensure()
and PyGILState_Release()
done as follows:
.../* Make sure we own the GIL */pygilstate_state state = Pygilstate_ensure ();/* Use functions in the interpreter */.../* Restore previous GIL state and return */pygilstate_release, ....
In high-level programs that involve C and Python, it's common to do a lot of things together-probably a mix of C, Python, c threads, Python threads. As long as you ensure that the interpreter is properly initialized and that the C code involved in the interpreter performs the correct Gil Management, there should be no problem.
Note that the call PyGILState_Ensure()
does not immediately preempt or interrupt the interpreter. If there is another code executing, the function is interrupted knowing that execution code frees the Gil. Internally, the interpreter performs periodic thread switching, so that if other threads are executing, the caller will eventually be able to run (though it might be a few first).
"Python coolbook" C Extension Library _ its six _ threads