python / psf-infra-meta

Meta-repository for PSF backed or managed systems. Created mainly for the issue tracker :)
10 stars 9 forks source link

embedded python hang, is there a way to restart without restarting the process? #104

Closed wlei2021 closed 3 years ago

wlei2021 commented 3 years ago

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): 1) Reinitialize through Py_FinalizeEx() and Py_Initialize() 2) Create an interrupt via PyThreadState_SetAsyncExc(threadID, PyExc_Exception) 3) Use Py_Exit(0) 4) SetKeyboard Interrupt, e.g.: PyGILState_STATE state; state = PyGILState_Ensure(); PyErr_SetString(PyExc_KeyboardInterrupt, "Abort"); - PyGILState_Release(state); 5) 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.

matrixise commented 3 years ago

Sorry, but this bug tracker is not for Python itself, for that, you can use this page: https://discuss.python.org/c/users/7