3

I want to wrap an existing C++ library which involves a blocking message loop and calling handler functions for Python using boost::python. E.g.:

import my_boostpython_lib

def my_handler_fn():
    do_something()

md = my_boostpython_lib.message_dispatcher()

# calls a C++ object method and blocks
md.run_message_loop(my_handler_fn)

Calling a Python function from C++ is no problem but the message loop needs to release the GIL since otherwise it would block the whole Python interpreter (see here, here and a related boost ticket)

As stated here it's important to lock the GIL again before calling a Python function.

In principle this sounds comprehensible to me but I wonder if there are any elegant solutions out there which show how this can be done. E.g. it would be very nice if I had to modify the boost::python wrapper only (instead of altering the library I want to wrap)

Do you know any working example which involves boost::python, callbacks using an object oriented approach (and maybe blocking functions which release the GIL) where I can copy some best practices from?

Community
  • 1
  • 1
frans
  • 8,868
  • 11
  • 58
  • 132
  • 1
    This [answer](http://stackoverflow.com/a/20828366/1053968) demonstrates how to safely wrap a Python callback in a handler and covers some subtle details, such as GIL management during copy-construction and destruction. – Tanner Sansbury Jul 17 '15 at 14:40

0 Answers0