Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realtime processing and callbacks with Python and C++

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

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?

like image 693
Doughy Avatar asked May 31 '10 22:05

Doughy


People also ask

What is callback in C?

A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time [Source : Wiki]. In simple language, If a reference of a function is passed to another function as an argument to call it, then it will be called as a Callback function.

How do you call a function in Python in C?

To call the Python function with no arguments, you must pass an empty tuple. For example: object *arglist; object *result; ... /* Time to call the callback */ arglist = mktuple(0); result = call_object(my_callback, arglist); DECREF(arglist);

What is the advantage of callback function in C?

The main advantage of using callbacks is that you can call a subroutine defined in higher software level from a lower software level subroutine.

What are Python callbacks?

In Python, a callback is simply a function or a method passed to LocalSolver. A callback takes two parameters: the LocalSolver object that triggers the event and the type of the callback. It is possible to use the same callback method or object for multiple events or multiple LocalSolver instances.


1 Answers

Have a look at Boost.Python. Its tutorial starts here.

like image 177
ChristopheD Avatar answered Oct 05 '22 23:10

ChristopheD