Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单个线程上运行Pybind11 是否有可能让pybDun11在C++中的一个线程上启动Python解释器?我有一门课 初始化解释器并 调用python脚本中的函数 < >我想在C++创建的多个线程上调用这个函数。< /P> #include "pybin_Python_C++_Multithreading_Pybind11 - Fatal编程技术网

在单个线程上运行Pybind11 是否有可能让pybDun11在C++中的一个线程上启动Python解释器?我有一门课 初始化解释器并 调用python脚本中的函数 < >我想在C++创建的多个线程上调用这个函数。< /P> #include "pybin

在单个线程上运行Pybind11 是否有可能让pybDun11在C++中的一个线程上启动Python解释器?我有一门课 初始化解释器并 调用python脚本中的函数 < >我想在C++创建的多个线程上调用这个函数。< /P> #include "pybin,python,c++,multithreading,pybind11,Python,C++,Multithreading,Pybind11,在单个线程上运行Pybind11 是否有可能让pybDun11在C++中的一个线程上启动Python解释器?我有一门课 初始化解释器并 调用python脚本中的函数 < >我想在C++创建的多个线程上调用这个函数。< /P> #include "pybind11/pybind11.h" #include <iostream> #include <thread> namespace py = pybind11; class callMyPythonFunctionFr

在单个线程上运行Pybind11

是否有可能让pybDun11在C++中的一个线程上启动Python解释器?我有一门课

  • 初始化解释器并
  • 调用python脚本中的函数
  • < >我想在C++创建的多个线程上调用这个函数。< /P>
    #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;
    }
    
    #包括“pybind11/pybind11.h”
    #包括
    #包括
    名称空间py=pybind11;
    类callMyPythonFunctionFromCpp
    {
    callMyPythonFunctionFromCpp(){m_module=py::module::import(“mypython_脚本_文件”)}
    void myfunc()//尝试在多个线程上调用此函数。
    { 
    py::object res=m_module.attr(“myfunc”);
    }
    私人:
    py::作用域_解释器m_-guard;
    py::模m_模;
    };
    void thread_函数()
    {
    从CPP m调用MyPythonFunction;
    m、 myfunc();
    
    std::cout强烈依赖于
    mypython\u script\u file
    所做的事情。几个资源(文件、信号、全局C变量)保持共享,就像在任何其他线程程序中一样,但是使用这种方法,任何访问控制手段都会丢失,因为Python模块不知道它已经被线程化了。其中一些问题可以通过在主程序上进行初始导入来解决,允许C初始化在启动任何线程之前完成运行。您好,Wim,我可以吗你对这篇文章有什么想法吗?非常感谢,