Similarly to the question discussed here, I need to call asynchronously python code from a multithreaded C++ program. But it will not be the main thread (the one that calls Py_Initialize()
and PyEval_InitThreads()
) that will call for the python execution.
I don't know how to manage the calls to PyEval_SaveThread()
and PyEval_RestoreThread()
: is it safe to call PyEval_SaveThread()
and leave the retrieved thread state unused? Would a call to PyThreadState_Clear()
make sense?
I would like to only manage the GIL using PyGILState_Ensure()
and PyGILState_Release()
if possible.
Thank you!
Edit 1:
I tried that in the main thread:
PyEval_InitThreads();
Py_Initialize();
PyThreadState* _state = PyEval_SaveThread();
PyEval_AcquireLock();
PyThreadState_Clear(_state);
PyEval_ReleaseLock();
I get a segmentation fault.
Edit 2:
I did not find a way to abandon the PyThreadState
just after the PyEval_SaveThread()
, but I confirm that it is possible to only deal with
PyGILState_Ensure()
and PyGILState_Release()
to protect a python execution once the thread is saved, no matter if we are still in the same thread or not.
At the end, it seems mandatory to restore the thread before finalization.
Edit 3:
- Today, using Python 2.7, moving to Python 3 in a near future.
- regarding the segmentation fault while clearing the python thread state, there is no error message. The seg fault appears at finalization. Python may guess that it has to close the main thread that I just cleared?
- When the program terminate, if no error occurred, the python executions are over, and then it should be able to finalize nicely. It is the way it runs, when I do not clear the thread state.