Pybind11:为什么从Python进行的asyn调用不能在C ++中正确执行回调? [英] Pybind11: Why doesn't asyn call from Python execute the callbacks properly in C++?

查看:221
本文介绍了Pybind11:为什么从Python进行的asyn调用不能在C ++中正确执行回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样实现的Python方法:

I have a Python method which is implemented like this:

def start(self):
    try:
        self.is_running = True
        self._main_loop()

    except Exception as ex:
        path='exceptions-servicecore.log'
        track = traceback.format_exc()
        exception_time = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
        with open(path, 'a') as f:
            f.writelines(f'\n{exception_time} : exception occured {ex.args} \n{track}')

def start_async(self):
    st = threading.Thread(target=self.start) 
    st.start()

此处的 _main_loop()然后运行一堆命令并执行从C ++ / C#等发送的回调。使用 Pybind11 我只是这样叫 start_async

The _main_loop() here then runs bunch of commands and executes the callbacks which are sent from C++/C# etc. Using Pybind11 I simply call start_async like this :

this->startFuncAsync = this->obj.attr("start_async");
...
CORE_API void Core::Start(bool async)
{
    try
    {
        if (async)
        {
            this->startFuncAsync();
        }
        else
        {
            this->startFunc();
        }
    }
    catch (std::exception ex)
    {
        std::cout << ex.what() << std::endl;
    }
}

但是看来,它不能正常运行。

But it seems, it just doesn't run properly.

它在Python中工作正常,但是当涉及到C ++或C#时,调用上面的 C ++ 函数不会显示任何输出!当我将其执行记录到文本文件中时,调用回调的Python部分会起作用。但是客户端没有活动。我看不到用C ++或C#生成的任何输出。回调正在工作,并在控制台(C ++或C#)上输出所需的信息。非异步功能( start )可以正常工作。

It works fine in Python, but when it comes to C++ or C# which call the C++ function above it doesn't show any output! The Python sections which call the callbacks do work as I log their execution to a text file. however there is no activity on the client sides. I don't see any outputs to be produced in C++ or C#. The callbacks are working and outputting the needed information on the console (C++ or C#). The non-async function (start) however, works just fine.

所以我的问题是,这是预期的行为吗?不管该函数是在Python中还是在外部调用,都不能正常工作吗?

So my question is, is this an expected behavior? Should this not work regardless of how the function is invoked in Python or outside of it?

推荐答案

此问题恰好与 Pybind11 看来。也就是说,只有一个执行线程可以工作,并且所有操作都必须在同一线程下执行。

如果该方法是像python一样作为单独的线程在python中启动的,则调用其他方法在c ++中,它将使用不同的线程,因此您可以与它进行交互,它将变得像僵尸线程,因为您将无法与它进行交互甚至停止它。

逻辑方式是在您可以控制线程的C ++端执行此操作。

This issue happened to be related to Pybind11 it seems. That is, only one thread of execution can work and all operations must executed under one same thread.
If the method is started as a separate thread in python like what we have here (start), calling other methods in c++ will use a different thread and thus you can interact with it, it will become like a zombie thread, as you will not be able to interact with it or even stop it.
The logical way would be to execute this in C++ side where you have control over the thread.

这篇关于Pybind11:为什么从Python进行的asyn调用不能在C ++中正确执行回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆