导入模块死锁(使用 Py_NewInterpreter)

use*_*971 6 python python-3.x

我正在编写一个具有嵌入式 Python 解释器的应用程序,但也可以从应用程序内运行脚本文件。

我想要归档的是有一种方法可以重置解释器,或者它可以在不影响全局环境的情况下执行文件/语句,这样您就可以运行脚本,编辑文件并再次运行脚本,而无需在中间完成Python(或重置我的口译员)。

我尝试创建一个新的解释器,但有时它会挂起整个应用程序(死锁)。这是一个小演示来查看问题。

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);
Run Code Online (Sandbox Code Playgroud)

如果我保存 GIL 状态,它有时会起作用,但根据文档,不支持与 Py_NewInterpreter 一起使用。

使用Python 3.4。

是否有其他解决方案可以在单独的环境中运行命令或修复死锁?