Blocking an import module (using Py_NewInterpreter)

I am writing an application that has a built-in Python interpreter, but can also run script files from within the application.

What I want to archive is to have a way to reset the interpreter or that it can execute the file / statement without affecting the global environment, so you can run the script, edit the file and run the script again without ending python between them (or reset my interpreter).

I tried to create a new interpreter, but sometimes it freezes the entire application (deadlock). Here is a small demo to see the problem.

Py_Initialize();
PyThreadState* pGlobalThreadState = PyThreadState_Get();
PyThreadState* pInterpreterThreadState = Py_NewInterpreter();
PyThreadState_Swap(pInterpreterThreadState);

PyRun_SimpleString("import PySide"); // Importing PySide deadlocks

Py_EndInterpreter(pInterpreterThreadState);
PyThreadState_Swap(pGlobalThreadState);

If I save GIL states, it sometimes works, but it is not supported along with Py_NewInterpreter according to the documentation.

Using Python 3.4.

?

+4

Source: https://habr.com/ru/post/1540677/


All Articles