Skip to content

Instantly share code, notes, and snippets.

@ludflu
Created July 9, 2014 15:55
Show Gist options
  • Save ludflu/7b626fa9dc54bb86ed21 to your computer and use it in GitHub Desktop.
Save ludflu/7b626fa9dc54bb86ed21 to your computer and use it in GitHub Desktop.
python -> C++ callback
#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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment