pass callback from python to c++ using boost::python
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 functionPyObject *py_callback;#the following function will invoked from python to populate the call back referencePyObject *set_py_callback(PyObject *callable){ py_callback = callable; /* Remember new callback */ return Py_None;}...#Initialize and acquire the global interpreter lockPyEval_InitThreads();#Ensure that the current thread is ready to call the Python C API PyGILState_STATE state = PyGILState_Ensure();#invoke the python functionboost::python::call<void>(py_callback);#release the global interpreter lock so other threads can resume executionPyGILState_Release(state);
The python function is invoked from C++, and executes as expected.
These test files from the boost::python source code repository contains great examples about how to pass callbacks from Python into C++: