将回调从 python 传递给 c++使用boost::python

发布于 2024-12-01 16:55:35 字数 980 浏览 0 评论 0 原文

我想将回调从我的 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2024-12-08 16:55:35

好吧,我仍在尝试解决这个问题,但到目前为止,这对我有用:

#this is the variable that will hold a reference to the python function
PyObject *py_callback;

#the following function will invoked from python to populate the call back reference
PyObject *set_py_callback(PyObject *callable)
{
    py_callback = callable;       /* Remember new callback */
    return Py_None;
}
...
#Initialize and acquire the global interpreter lock
PyEval_InitThreads();

#Ensure that the current thread is ready to call the Python C API 
PyGILState_STATE state = PyGILState_Ensure();

#invoke the python function
boost::python::call<void>(py_callback);

#release the global interpreter lock so other threads can resume execution
PyGILState_Release(state);

Python 函数是从 C++ 调用的,并按预期执行。

Ok, I'm still trying to figure this out too, but here's whats working for me so far:

#this is the variable that will hold a reference to the python function
PyObject *py_callback;

#the following function will invoked from python to populate the call back reference
PyObject *set_py_callback(PyObject *callable)
{
    py_callback = callable;       /* Remember new callback */
    return Py_None;
}
...
#Initialize and acquire the global interpreter lock
PyEval_InitThreads();

#Ensure that the current thread is ready to call the Python C API 
PyGILState_STATE state = PyGILState_Ensure();

#invoke the python function
boost::python::call<void>(py_callback);

#release the global interpreter lock so other threads can resume execution
PyGILState_Release(state);

The python function is invoked from C++, and executes as expected.

时光倒影 2024-12-08 16:55:35

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++:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文