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?
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.
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);
The main advantage of using callbacks is that you can call a subroutine defined in higher software level from a lower software level subroutine.
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.
Have a look at Boost.Python. Its tutorial starts here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With