I have embedded Python3 in my big C++ application. Python gives the user script capability for custom data processing.
Problem : I have many threads that interact with Python and I don't really get how to protect my code with GIL. So far, the only way I made my code work is using boost::mutex
.
Here is a very simplified example that reproduces my problem:
- Thread A calls first
Init()
to initialize Python (static function). - Thread B calls
Pythonize()
to do some work on Python. Thread B is blocked on the first call for locking GIL.
Code:
#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();
}
it is deadlocking in the first lock call. The console shows: Locking...
The "OK" is never printed. What am I doing wrong ?
EDIT : corrected code, now it is working. I needed to release the GIL from the main thread.