以下是在多线程程序中使用Python解释器的示例:
#include <python.h>
#include <boost/thread.hpp>
void f(const char* code)
{
static volatile auto counter = 0;
for(; counter < 20; ++counter)
{
auto state = PyGILState_Ensure();
PyRun_SimpleString(code);
PyGILState_Release(state);
boost::this_thread::yield();
}
}
int main()
{
PyEval_InitThreads();
Py_Initialize();
PyRun_SimpleString("x = 0\n");
auto mainstate = PyEval_SaveThread();
auto thread1 = boost::thread(f, "print('thread #1, x =', x)\nx += 1\n");
auto thread2 = boost::thread(f, "print('thread #2, x =', x)\nx += 1\n");
thread1.join();
thread2.join();
PyEval_RestoreThread(mainstate);
Py_Finalize();
}
看起来不错,但不同步。 Python解释器在PyRun_SimpleString期间多次释放并重新获取GIL(请参阅文档,第#2页 http://docs.python.org/py3k/c-api/init.html#thread-state-and-the-global-interpreter-lock).
我们可以使用自己的同步对象来序列化 PyRun_SimpleString 调用,但这是一种错误的方式。
Python 有自己的同步模块 -_thread
and threading
。但它们在这段代码中不起作用:
Py_Initialize();
PyRun_SimpleString(R"(
import _thread
sync = _thread.allocate_lock()
x = 0
)");
auto mainstate = PyEval_SaveThread();
auto thread1 = boost::thread(f, R"(
with sync:
print('thread #1, x =', x)
x += 1
)");
- 它会产生一个错误
File "<string>", line 3, in <module> NameError: name '_[1]' is not defined
和僵局。
如何最有效地同步嵌入式Python代码?