是否有可能让pybDun11在C++中的一个线程上启动Python解释器?我有一门课
我想在C++创建的多个线程上调用这个函数。
#include "pybind11/pybind11.h"
#include <iostream>
#include <thread>
namespace py = pybind11;
class callMyPythonFunctionFromCpp
{
callMyPythonFunctionFromCpp() { m_module = py::module::import("mypython_script_file"); }
void myfunc() // trying to call this function on multiple threads.
{
py::object res = m_module.attr("myfunc");
}
private:
py::scoped_interpreter m_guard;
py::module m_module;
};
void thread_function()
{
callMyPythonFunctionFromCpp m;
m.myfunc();
std::cout << "thread function\n";
}
int main()
{
std::thread t(&thread_function);
// ...
return 0;
}
目前没有回答
相关问题 更多 >
编程相关推荐