Pybind11: Why doesnt asyn call from python execute the callbacks properly in C++?

人盡茶涼 提交于 2020-05-17 06:12:25

问题


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()

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.
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 out-puting the needed information on the console (C++ or C#).
The non-async function (start) however, works just fine.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/61168438/pybind11-why-doesnt-asyn-call-from-python-execute-the-callbacks-properly-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!