I have a C++ program that uses the C api to use a Python library of mine. Both the Python library AND the C++ code are multithreaded.
In particular, one th
A correct order of steps to perform what you are trying to do is:
In the main thread:
Py_Initialize*.PyEval_InitThreads().At this point, the main thread still holds the GIL.
PyGILState_Ensure(). PyGILState_Release().Because the main thread holds the GIL, this thread will be waiting to acquire the GIL. If the main thread calls the Python API it may release the GIL from time to time allowing the Python thread to execute for a little while.
PyEval_SaveThread()PyEval_RestoreThread()I suspect that you are missing the last step - releasing the GIL in the main thread, allowing the Python thread to execute.
I have a small but complete example that does exactly that at this link.