0

I have a C++ software that initializes a Python interpreter using the C-API and runs "scripts" (from files) on user demand. Additionally I have defined a number of "callback" functions using SWIG so the python scripts can access data and operations of the main C++ program. The scripts are started using the PyRun_SimpleFile API call while holding on to the GIL.

As a next step I would like to add async event handling: The python script should be able to "subscribe" to events by providing the desired event parameters and a string (or callback function) to execute when the event has occurred. This callback string or function shall be started in a new python thread - kind of an ISR.

As far as I understood the GIL operations, when starting the script using PyRun_SimpleFile I need to hold on to the GIL. This call does not return until the execution has finished. The event notification thread therefore cannot acquire the GIL and call PyRun_SimpleString (to spawn a thread) when the event occurs.

Is there a neat way for starting a background thread in python from C++ when the main thread of python is already executing?

My "quick and dirty" solution would be to use the main thread as a "stepping stone" for operations, meaning that I start the "script" in a thread already and therefore the initial PyRun_SimpleFile would return immediately thus enabling me to start other threads meanwhile. Is there a better way?

TeBe
  • 1

0 Answers0