[英]How can I start a Python thread FROM C++?
Note that I'm constrained to use Python 2.6. 请注意,我被限制使用Python 2.6。 I have a Python 2.6 application that uses a C++ multi-threaded API library built with boost-python.
我有一个Python 2.6应用程序,它使用一个用boost-python构建的C ++多线程API库。 My use-case was simply to execute a Python function callback from a C++ boost thread but despite the many different attempts and researching all the available online resources I haven't found any way that works.
我的用例只是从C ++ boost线程执行Python函数回调,但尽管有许多不同的尝试和研究所有可用的在线资源,我还没有找到任何有效的方法。 All the proposed solutions revolve around a different combination of the functions:
Py_Initialize*
, PyEval_InitThreads
, PyGILState_Ensure
, PyGILState_Release
but after trying all possible combinations nothing works in practice eg 所有提出的解决方案围绕着函数的不同组合:
Py_Initialize*
, PyEval_InitThreads
, PyGILState_Ensure
, PyGILState_Release
但是在尝试所有可能的组合之后,在实践中没有任何作用,例如
Therefore, this question: how can I start and run a Python thread from C++ ? 因此,这个问题:如何从C ++启动和运行Python线程? I basically want to: create it, run it with a Python target function object and forget about it.
我基本上想要:创建它,用Python目标函数对象运行它并忘记它。
Is that possible? 那可能吗?
Based on the text below from your question: 根据您问题的以下文字:
Run a Python thread from C++?
从C ++运行Python线程? I basically want to: create it, run it with a Python target function object and forget about it.
我基本上想要:创建它,用Python目标函数对象运行它并忘记它。
You may find useful to simply spawn a process using sytem
: 您可能会发现使用
sytem
简单生成进程非常有用:
system("python myscript.py")
And if you need to include arguments: 如果你需要包含参数:
string args = "arg1 arg2 arg3 ... argn"
system("python myscript.py " + args)
You should call PyEval_InitThreads
from your init routine (and leave the GIL held). 您应该从您的init例程调用
PyEval_InitThreads
(并保留GIL)。 Then, you can spawn a pthread (using boost), and in that thread, call PyGILState_Ensure
, then call your python callback ( PyObject_CallFunction
?), release any returned value or deal with any error ( PyErr_Print
?), release the GIL with PyGILState_Release
, and let the thread die. 然后,你可以生成一个pthread(使用boost),并在该线程中调用
PyGILState_Ensure
,然后调用你的python回调( PyObject_CallFunction
?),释放任何返回值或处理任何错误( PyErr_Print
?),用PyGILState_Release
释放GIL,让线程死掉
void *my_thread(void *arg)
{
PyGILState_STATE gstate;
PyObject *result;
gstate = PyGILState_Ensure();
if ((result = PyObject_CallFunction(func, NULL))) {
Py_DECREF(result);
}
else {
PyErr_Print();
}
PyGILState_Release(gstate);
return NULL;
}
The answer to the OP How to call Python from a boost thread? OP的答案如何从boost线程调用Python? also answers this OP or DP (Derived:)).
也回答了这个OP或DP(Derived :))。 It clearly demonstrates how to start a thread from C++ that callbacks to Python.
它清楚地演示了如何从回调Python的C ++启动一个线程。 I have tested it and works perfectly though needs adaptation for pre-C++11.
虽然需要适应前C ++ 11,但我已经对它进行了测试并且工作正常 。 It uses Boost Python, is indeed an all inclusive 5* answer and the example source code is here .
它使用Boost Python,确实是一个全包的5 *答案, 示例源代码就在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.