Python GIL和线程

pou*_*ill 5 c++ python-c-api gil python-3.x

我已经在大型C ++应用程序中嵌入了Python3。Python提供了用于自定义数据处理的用户脚本功能。
问题:我有许多与Python交互的线程,但是我真的不知道如何使用GIL保护我的代码。到目前为止,使代码工作的唯一方法是使用boost::mutex

这是一个非常简化的示例,重现了我的问题:

  • 线程A首先调用Init()以初始化Python(静态函数)。
  • 线程B调用Pythonize()在Python上做一些工作。第一次锁定GIL时,线程B被阻塞。

代码

#include <iostream>
#include <boost/thread.hpp>
#include <boost/bind.hpp>

#include "Python.h"

struct RTMaps_GILLock
{
    RTMaps_GILLock()
    {
        std::cout << "Locking..." << std::endl;
        m_state = PyGILState_Ensure();
    }

    ~RTMaps_GILLock()
    {
        std::cout << "Unlocking..." << std::endl;
        PyGILState_Release(m_state);
    }

private:
    PyGILState_STATE m_state;
};
#define GILLOCK RTMaps_GILLock lock;

class PythonEmbed
{
public:
    static void Init()
    {
        Py_Initialize();
        // EDIT : adding those two lines made my day :
        PyEval_InitThreads(); // This acquires GIL
        PyEval_SaveThread(); // Release the GIL
    }

    void Pythonize()
    {
        GILLOCK;
        // Never goes here :(
        std::cout << "OK" << std::endl;
    }
};

int main()
{
    PythonEmbed::Init();

    PythonEmbed pyt;
    boost::thread t(boost::bind(&PythonEmbed::Pythonize, pyt));

    t.join();
}
Run Code Online (Sandbox Code Playgroud)

它在第一个锁调用中处于死锁状态。控制台显示:锁定中...

永远不会打印“确定”。我究竟做错了什么 ?

编辑:更正的代码,现在可以正常工作。我需要从主线程释放GIL。

Leo*_*ini 4

我遇到了你的确切问题,请确保不要从主线程(初始化 Python 的线程)调用 PyGILState_Ensure() ,因为它需要完全不同的调用。我已经结束设置线程映射器,每次调用 acquirePython() 都会检查哪个线程正在调用它,如果它是主线程,它会使用:

PyEval_SaveThread();

否则它会存储 GIL。这些是我班级的相关部分:

void MManager::acquirePython(void) {
    MThread thisThread = MFramework::MProcesses::GetCurrentThread();
    if (thisThread != mainThread) {
        Lock();
        std::map<MThread,void*>::iterator i = threadStates.find(thisThread);
        if (i == threadStates.end()) {
            Unlock();
            PyGILState_STATE gstate = PyGILState_Ensure();
            _PyGILState_STATE_* encState = new _PyGILState_STATE_;
            encState->state = gstate;
            encState->refCount = 1;
            Lock();
            threadStates[thisThread] = encState;
            Unlock();
        } else {
            _PyGILState_STATE_* encState = (_PyGILState_STATE_*)i->second;
            encState->refCount = encState->refCount + 1;
            Unlock();
        }

    } else {
        if (mainThreadState) PyEval_RestoreThread((PyThreadState*)mainThreadState);
    }

}

void MManager::releasePython(void) {
    MThread thisThread = MFramework::MProcesses::GetCurrentThread();
    if (thisThread != mainThread) {
        Lock();
        std::map<MThread,void*>::iterator i = threadStates.find(thisThread);
        if (i != threadStates.end()) {
            _PyGILState_STATE_* encState = (_PyGILState_STATE_*)i->second;
            if (encState->refCount <= 1) {
                threadStates.erase(i);
                Unlock();

                PyGILState_Release(encState->state);
                delete encState;
            } else {
                encState->refCount = encState->refCount - 1;
                Unlock();
            }
        } else {
            Unlock();
        }

    } else {
        mainThreadState = PyEval_SaveThread();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @LeonardoBernardini:然后*然后*,OP 需要从主线程释放 GIL,因为 InitThreads 获取了它。 (2认同)