嵌入式Python应用程序中Py_Finalize期间的致命错误

时间:2014-11-29 20:57:06

标签: python c++ multithreading

感谢您对此的帮助 - 多次询问此问题的变体,但我还没有找到完整答案。我正在使用MS MFC类将嵌入式Python 3.4.2添加到用C ++编写的现有模拟器工具中。该应用程序是多线程的,因此用户可以执行Python脚本并与模拟器系统进行交互。

如何成功退出? 我是否以正确的顺序使用GIL和线程状态命令? 我是否过早地结束了Python解释器线程并打破了Python线程整合机制?

我的问题是,当我调用Py_Finalize时,它调用wait_for_thread_shutdown,然后调用PyThreadState_Get,并发出致命错误," PyThreadState_Get:没有当前线程。"基于检测到致命错误的点,它似乎与多线程嵌入式Python应用程序末尾的线程合并有关。

我已经缩小了我的代码以澄清它并消除任何看似不相关的内容。如果我走得太远或不够远,我道歉。主线程初始化并完成Python。

BOOL CSimApp::InitInstance()
{
    ...
    // Initialize command table for appl. object and for documents
    int iReturn = PyImport_AppendInittab("sim", &PyInit_SimApp);
    iReturn = PyImport_AppendInittab("sim_doc", &PyInit_SimDoc);

    // Initialize Python and prepar to create threads
    _pHInstance = new CPyInstance();
    ...
}

int CSimApp::ExitInstance() 
{
    ...
    if (_pHInstance) {
        delete _pHInstance;
        _pHInstance = NULL;
    }
    ...
}

我使用实用程序类来创建Python实例(CPyInstance)并管理Python GIL(ACQUIRE_PY_GIL)。初始化应用程序时,还会创建CPyInstance的实例。 CPyInstance类初始化并完成Python线程管理。 Python全局锁管理是使用ACQUIRE_PY_GIL和RELEASE_PY_GIL结构完成的。

class CPyInstance
{
public:
    CPyInstance();
    ~CPyInstance();
    static PyThreadState * mainThreadState;
};

inline CPyInstance::CPyInstance()
{
    mainThreadState = NULL;
    Py_Initialize();
    PyEval_InitThreads();
    mainThreadState = PyThreadState_Get();
    PyEval_ReleaseLock();
}

inline CPyInstance::~CPyInstance()
{
    Py_Finalize();
}

static CPyInstance    *_pHInstance = NULL;

int PyExit()
{
    if (_pHInstance) {
        delete _pHInstance;
        _pHInstance = NULL;
    }
    return 0;
}

struct ACQUIRE_PY_GIL {
    PyGILState_STATE state;
    ACQUIRE_PY_GIL() { state = PyGILState_Ensure(); }
    ~ACQUIRE_PY_GIL() { PyGILState_Release(state); }
};

struct RELEASE_PY_GIL {
    PyThreadState *state;
    RELEASE_PY_GIL() {  state = PyEval_SaveThread(); }
    ~RELEASE_PY_GIL() { PyEval_RestoreThread(state); }
};

创建Python解释器线程以响应CMainFrame窗口处理的Windows消息。 Python线程和解释器运行以响应用户命令。当用户使用解释器(Control-Z)完成时,解释器退出,线程清除并删除Python线程状态,然后线程自行终止。

void CMainFrame::OnOpenPythonInterpreter()
{
    // Create PyThread thread
    m_pPyThread = (CPyThread*)AfxBeginThread(RUNTIME_CLASS(CPyThread),
                    THREAD_PRIORITY_BELOW_NORMAL,0, CREATE_SUSPENDED);
    CMainFrame* mf = (CMainFrame*)theApp.m_pMainWnd;
    m_pPyThread->SetOwner(this,((CWnd*)mf)->GetSafeHwnd());
    m_pPyThread->CreateLocks(&m_PyThreadEvent,&m_PyThreadBusyMutex);
    m_pPyThread->ResumeThread();
}

CPyThread类实际上调用Python解释器。当解释器返回时,释放GIL并清除并删除Python线程状态。该线程终止以响应PostQuitMessage。

int CPyThread::Run() 
{
    PyEval_AcquireLock();
    PyInterpreterState * mainInterpreterState = CPyInstance::mainThreadState->interp;
    PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState);
    PyEval_ReleaseLock();

    try {
        ACQUIRE_PY_GIL    lock;
        FILE* fp1 = stdin;
        char *filename = "Embedded";
        PyRun_InteractiveLoop(fp1, filename);
    } catch(const std::exception &e) {
        safe_cout << "Exception in PyRun_InteractiveLoop: " << e.what() << "\n";
    } catch(...) {
        std::cout << "Exception in Python code: UNKNOWN\n";
    }

    PyThreadState_Clear(myThreadState);
    PyThreadState_Delete(myThreadState);

    ::PostQuitMessage(0);
    return 0;
}

int CPyThread::ExitInstance() 
{
    return CWinThread::ExitInstance();
}

根据&#34; user4815162342&#34;的建议在调用Py_Finalize()之前,我修改了~CPyInstance()析构函数以获取GIL。现在我的应用程序似乎正常退出,谢谢。

inline CPyInstance::~CPyInstance()
{
    try {
        PyGILState_STATE state = PyGILState_Ensure();
        Py_Finalize();
    } catch(const std::exception &e) {
        safe_cout << "Exception in ~CPyInstance(): " << e.what() << "\n";
    } catch(...) {
        std::cout << "Exception in Python code: UNKNOWN\n";
    }
}

1 个答案:

答案 0 :(得分:0)

您在没有持有全局解释器锁的情况下调用Py_Finalize。这是不允许的:必须为每个Python API调用保持锁定,只有获取GIL本身的调用的例外。

ACQUIRE_PY_GIL RAII后卫对此目的无用,因为它会在Py_Finalize返回后尝试释放GIL - 在这种情况下,您必须在没有匹配版本的情况下调用PyGILState_Ensure