使用Python和C ++实时处理和回调 [英] Realtime processing and callbacks with Python and C++

查看:200
本文介绍了使用Python和C ++实时处理和回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写代码来做一些计算复杂的实时处理。我想创建一些Python类来管理我的所有脚本,并留下算法的密集部分编码在C + +,使他们可以尽可能快地运行。我想在Python中实例化对象,并有C ++算法chime回到脚本与回调在python中。像:

I need to write code to do some realtime processing that is fairly computationally complex. I would like to create some Python classes to manage all my scripting, and leave the intensive parts of the algorithm coded in C++ so that they can run as fast as possible. I would like to instantiate the objects in Python, and have the C++ algorithms chime back into the script with callbacks in python. Something like:

myObject = MyObject()
myObject.setCallback(myCallback)
myObject.run()

def myCallback(val):
    """Do something with the value passed back to the python script."""
    pass

这是否可能?如何在运行在C ++模块中的循环中运行python中的回调?任何人都有链接或教程,以帮助我这样做这正确吗?

Will this be possible? How can I run a callback in python from a loop that is running in a C++ module? Anyone have a link or a tutorial to help me do this correctly?

推荐答案

我建议使用由ChristopheD建议使用Boost.Python。如果C ++扩展在它自己的线程上下文中运行(不是由Python创建的),就会有困难。如果是这种情况,请务必使用 PyGILState_Ensure( ) PyGILState_Release() 函数。

I suggest using Boost.Python as suggested by ChristopheD. A gotcha would be if the C++ extension is running in it's own thread context (not created by Python). If that's the case, make sure to use the PyGILState_Ensure() and PyGILState_Release() functions when calling into Python code from C++.

从文档( http://docs.python.org/c-api/init.html#thread-state-and-the-global-interpreter-lock ):


从版本2.3开始,线程
现在可以利用
PyGILState _ *()函数自动完成上述所有的
操作。从C
线程调用Python的典型的
现在是:

Beginning with version 2.3, threads can now take advantage of the PyGILState_*() functions to do all of the above automatically. The typical idiom for calling into Python from a C thread is now:

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

/* Perform Python actions here.  */
result = CallSomeFunction();
/* evaluate result */

/* Release the thread. No Python API allowed beyond this point. */
PyGILState_Release(gstate)


回调短&甜 - 限制在C ++代码中执行异常处理的需要。如果你使用wxPython,你可以使用强大的异步事件系统。或者回调可以将事件放在一个队列,你可以有一个线程专门用于异步执行回调/事件代码。

I recommend making the callbacks short & sweet - to limit the need to perform exception handling in C++ code. If you're using wxPython, you could use it's robust async event system. Or the callbacks could put events on a Queue and you could have a thread devoted to asynchronously executing callback/event code.

即使Boost.Python魔法,你会有在处理线程时熟悉Python C API的这一部分。 (不要忘记用 Py_BEGIN_ALLOW_THREADS Py_END_ALLOW_THREADS 包装C ++函数以释放GIL!)

Even with Boost.Python magic, you'll have to get familiar with this portion of the Python C API when dealing with threads. (Don't forget to wrap the C++ functions with Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS to release the GIL!)

这篇关于使用Python和C ++实时处理和回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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