我在我的大C ++应用程序中嵌入了Python3。 Python为自定义数据处理提供了用户脚本功能
问题:我有许多与Python交互的线程,我真的不知道如何用GIL保护我的代码。到目前为止,我使代码工作的唯一方法是使用boost::mutex
。
这是一个非常简单的例子,可以重现我的问题:
Init()
来初始化Python(静态函数)。Pythonize()
在Python上做一些工作。线程B在第一次锁定GIL时被阻止。代码:
#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();
}
它在第一次锁定调用中死锁。控制台显示: 锁定...
&#34; OK&#34;永远不会打印。我做错了什么?
编辑:更正了代码,现在它正在运行。我需要从主线程中释放GIL。答案 0 :(得分:4)
我有你的确切问题,请确保不要从主线程调用PyGILState_Ensure(),初始化Pythons,因为它需要一个完全不同的调用。 我已经设置了一个线程映射器,每次调用我的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();
}
}