简体   繁体   English

boost :: python-如何从C ++在自己的线程中调用python函数?

[英]boost::python - how to invoke a python function in its own thread from C++?

I have a module written in python. 我有一个用python编写的模块。 this module is sort of an interface to many different functionalities I implemented in Python: 这个模块是我在Python中实现的许多不同功能的接口:

EmbeddingInterface.py simply imports this module and creates an instance: EmbeddingInterface.py只需导入此模块并创建一个实例:

import CPPController

cppControllerInstance = CPPController()

I would like to use cppControllerInstance in c++. 我想在c ++中使用cppControllerInstance。 this is what I have done so far: 到目前为止,这是我所做的:

#include <Python.h>
#include <boost\python.hpp>

using namespace boost;

python::object createController()
{
    try
    {
        Py_Initialize();

        python::object mainModule = python::import("__main__");
        python::object mainNamespace = mainModule.attr("__dict__");

        python::dict locals;

        python::exec(
            "print \"loading python implementetion:\"\n"
            "import sys\n"
            "sys.path.insert(0, \"C:\\Projects\\Python\\ProjectName\\Panda\")\n"
            "import EmbeddingInterface\n"
            "controller = EmbeddingInterface.cppControllerInstance\n",
            mainNamespace, locals);

            python::object controller = locals["controller"];
            return controller;
    }
    catch(...) {}
}

The Problem: 问题:

This 'controller' has some functions which must be called asynchronously. 该“控制器”具有一些必须异步调用的功能。 its work is continuous and in addition it can throw exceptions. 它的工作是连续的,此外还可能引发异常。 which is why std::async sounded great. 这就是为什么std :: async听起来很棒的原因。

But it doesn't work: 但这不起作用:

int main()
{
    python::object controller = createController();
    python::object loadScene = controller.attr("loadScene");
    //loadScene(); // works OK but blocking!
    std::async(loadScene); // non blocking but nothing happens!
    while(true); // do some stuff
}

I tried to invoke the python function 'loadScene' with its own thread but the function seemed to be blocking. 我试图用自己的线程调用python函数'loadScene',但该函数似乎正在阻塞。 It never returns. 它永远不会回来。

What is the proper way of doing that? 这样做的正确方法是什么?

Seems you misunderstood the behavior of std::async 似乎您误解了std :: async的行为

a snippet of test code: 测试代码片段:

#include <iostream>
#include <chrono>
#include <thread>
#include <future>

int doSomething(){
  std::cout << "do something"<<std::endl;
  return 1;
}

int main(){
   auto f = std::async(doSomething);

   std::this_thread::sleep_for(std::chrono::seconds(3));
   std::cout <<"wait a while"<<std::endl;
   f.get();
   return 0;
}

Output: 输出:

wait a while
do something

change the line 换线

auto f = std::async(doSomething);

to

auto f = std::async(std::launch::async,doSomething);

Then output: 然后输出:

do something
wait a while

As your example, to run it immediately in another thread, you can try : 例如,要在另一个线程中立即运行它,可以尝试:

std::async(std::launch::async,loadScene);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (在Boost :: Python中)如何实例化python模块中定义的类的对象并从C ++调用其方法 - (in Boost::Python)How can I instantiate an object of a class defined in a python module and invoke its methods from C++ 如何使用 pybind11 在 C++ 线程中调用 Python 函数作为回调 - How to invoke Python function as a callback inside C++ thread using pybind11 如何从外语线程调用python函数(C ++) - How to call a python function from a foreign language thread (C++) 如何使用Boost Python将C ++ bool转换为Python布尔值? - How to convert from C++ bool to Python boolean with Boost Python? 为什么Python运行C ++函数比通过main()函数运行自己的函数的C ++更快? - Why does Python run a C++ function faster than C++ running its own function via its main() function? 如何在Boost Python中公开带有可变参数的C ++函数 - How to expose a c++ function taking variable arguments in boost python 将Python函数传递给Boost C ++ - Passing Python function to Boost C++ C ++和boost :: python - C++ and boost::python 如何使用QT / python从Javascript调用C ++函数? - how to invoke C++ functions from Javascript using QT/python? 如何从boost :: python :: list提取值到C ++ - How to extract values from boost::python::list to C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM