我想将回调从我的 python 代码传递到 c++
我希望我的代码看起来像这样:
在 C++ 中:
typedef void (*MyCallback_t) (CallbackInfo);
class MyClass
{...
void setcallback(MyCallback_t cb);
...
}
并在 python 中使用它:
import mylib
def myCallback(mylib_CallbackInfo):
...
t = mylib.MyClass()
t.setcallback(myCallback)
我看到了一些接近我的问题的主题,但无法解决它
例如这里:
使用 Python 和 C++ 进行实时处理和回调有建议使用 boost::python 并警告有关 GLI 但没有示例。
这里
如何调用 python 函数来自外语线程(C ++)没有对python代码部分和“BOOST_PYTHON_MODULE”部分的完整描述,
我还在Boost python howto 但它没有编译,实际上我不明白如何使用它。
I want to pass callback from my python code to c++
I want my code look something like this:
In C++ :
typedef void (*MyCallback_t) (CallbackInfo);
class MyClass
{...
void setcallback(MyCallback_t cb);
...
}
And to use it in python :
import mylib
def myCallback(mylib_CallbackInfo):
...
t = mylib.MyClass()
t.setcallback(myCallback)
I saw some topics near my problem but couldn't solve it
For example here :
Realtime processing and callbacks with Python and C++ there is advice to use boost::python and warning about GLI but no examples.
And here
How to call a python function from a foreign language thread (C++) there is no full description with python code part and with "BOOST_PYTHON_MODULE" part
I also found link to use py_boost_function.hpp for example in Boost python howto but it didn't compile and actualy I couldn't understand how to use it.
发布评论
评论(2)
好吧,我仍在尝试解决这个问题,但到目前为止,这对我有用:
Python 函数是从 C++ 调用的,并按预期执行。
Ok, I'm still trying to figure this out too, but here's whats working for me so far:
The python function is invoked from C++, and executes as expected.
boost::python 源代码存储库中的这些测试文件包含有关如何将回调从 Python 传递到 C++ 的精彩示例:
These test files from the boost::python source code repository contains great examples about how to pass callbacks from Python into C++: