I\'m embedding Python interpreter to a C program. However, it might happen that while running some python script via PyRun_SimpleString() will run into infinite
I wanted to be able to interrupt any actively running Python code block, including cancelling an infinite loop or just some long-running code. In my application, single-threaded Python code is submitted and evaluated. An interrupt request can come at any time. This question and answer were crucial in helping me actually achieve that.
Looking at this question in 2019, the solution here did not work for me; I tried to use Py_AddPendingCall(&quit, NULL); in quite a few different ways but the called function never executed as expected, or at all. This caused the Python engine to hang and eventually crash when I submitted Py_FinalizeEx(). I finally found what I needed in this very useful answer to a similar question.
After I start the Python engine, I retrieve the thread id using the threading package in Python code. I parse that into a c long value and save it.
The interrupt function is actually quite simple:
PyGILState_Ensure()
PyThreadState_SetAsyncExc(threadId, PyExc_Exception) using the thread ID that I save from earlier and a generic exception type
PyGILState_Release()