Detail C language and Python in the use of threads
- 2020-10-31 21:54:11
- OfStack
The problem
You have a program that USES a mixture of C, Python, and threads, some of which were created in C and are beyond the control of the Python interpreter. And some threads also use the functions in Python C API.
The solution
If you want to mix C, Python, and threads at 1 start, you need to make sure that the global interpreter lock (GIL) for Python is properly initialized and managed. To do this, put the following code in your C code and make sure it gets called before any threads are created.
#include <Python.h>
...
if (!PyEval_ThreadsInitialized()) {
PyEval_InitThreads();
}
...
For any call to the Python object or Python C API C code, make sure you get and release GIL correctly in the first place. It can be used
PyGILState_Ensure()
and
PyGILState_Release()
To do so, 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(state);
...
Each call
PyGILState_Ensure()
They all have to be called accordingly
PyGILState_Release()
.
discuss
In advanced programs involving C and Python, it is common to do many things at once -- possibly a mix of C, Python, C, and Python threads. As long as you make sure that the interpreter is properly initialized and that the C code involved in the interpreter performs the correct GIL administration, you should have no problem.
The thing to notice is the call
PyGILState_Ensure()
It does not immediately preempt or interrupt the interpreter. If any other code is executing, this function is interrupted until that execution code releases GIL. Internally, the interpreter performs a periodic thread switch, so if another thread is executing, the caller can eventually run (although it may have to wait for 1 first).
That is how to explain the mixing of threads in C and Python in detail. For more information on the mixing of threads in C and Python, please follow the other articles on this site!