We have an embedded python in our application. Our application has a python script window that allows user to execute script.
It can be executed in main thread (where the UI thread is) or a separate background thread.
We found an issue that certain python libraries such as scipy (e.g. import scipy) or any libraries that deal with the UI (such as matplotlib) needs to run in the main thread. If it is being run in the background thread, it would hang (in PyRun_SimpleString() call and never returns). After it is hang, there is no way to execute any more python script except restarting the complete application.
Seems like to be a problem with the embedded python that there is no way to restart the embedded python after it hangs in PyRun_SimpleString().
I have tried the following but they did not work (results in access violation etc):
- Reinitialize through Py_FinalizeEx() and Py_Initialize()
- Create an interrupt via PyThreadState_SetAsyncExc(threadID, PyExc_Exception)
- Use Py_Exit(0)
- SetKeyboard Interrupt, e.g.:
state = PyGILState_Ensure();
PyErr_SetString(PyExc_KeyboardInterrupt, “Abort”); -
- Create sub-interpreter to execute subsequent script
Looks like once the (main) embedded interpreter hangs in PyRun_SimpleString(), there is no way to recover without a restart of the application.