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 loop or execute for too long. Consider PyRun_SimpleString("while 1: pass");
In preventing the main program to block I thought I could run the interpreter in a thread.
How do I stop executing the python script in embedded interpreter running in a thread without killing the whole process?
Is it possible to pass an exception to the interpreter? Should I wrap the script under some other script which would listen to signals?
PS: I could run the python in a separate process but this is not what I want - unless it is the last resort...
Update:
So, it works now. Thank you Denis Otkidach, once again!
If I see this right, you have to do two things: tell the interpreter to stop and return -1
in the same thread as your PyRun_SimpleString() is running.
To stop, one has a few possibilities: PyErr_SetString(PyExc_KeyboardInterrupt, "...")
or PyErr_SetInterrupt()
- the first one might leave Python running a few more instructions and then it stops, the later one stops the execution immediately.
To return -1
you use Py_AddPendingCall()
to inject a function call into Python execution. The docs are mentioning it since version 2.7 and 3.1 but it runs on earlier Pythons as well (2.6 here). From 2.7 and 3.1 it should also be thread-safe, meaning you can call it without acquiring GIL (?).
So one could rewrite the example bellow:
int quit() {
PyErr_SetInterrupt();
return -1;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…