请注意,我只能使用 Python 2.6。我有一个 Python 2.6 应用程序,它使用通过 boost-python 构建的 C++ 多线程 API 库。我的用例只是从 C++ boost 线程执行 Python 函数回调,但尽管进行了许多不同的尝试并研究了所有可用的在线资源,但我还没有找到任何可行的方法。所有提议的解决方案都围绕着函数的不同组合:Py_Initialize*
、PyEval_InitThreads
、PyGILState_Ensure
、PyGILState_Release
但是在尝试了所有可能的组合之后,在实践中没有任何效果,例如
- Embedding Python in multi-threaded C++ applications代码 here
- PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseum)
因此,这个问题是:如何从 C++ 启动和运行 Python 线程?我基本上想要:创建它,使用 Python 目标函数对象运行它,然后忘记它。
这可能吗?
最佳答案
根据您问题中的以下文字:
Run a Python thread from C++? I basically want to: create it, run it with a Python target function object and forget about it.
您可能会发现使用 sytem
简单地生成一个进程很有用:
system("python myscript.py")
如果你需要包含参数:
string args = "arg1 arg2 arg3 ... argn"
system("python myscript.py " + args)
关于python - 如何从 C++ 启动 Python 线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41246071/