-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Threaded callbacks ? #1087
Comments
The main issue is that any time you go from c++ code back to Python (whether explicitly calling a Python function, or implicit operations such as creating new Python variables), you have to take out a gil lock. So as long as each wrapper function that the external library calls does this (by keeping a |
This may have little sense in terms of performance because at most one peace of python code could be executed in one process due to GIL anyway. |
I've tried adding gil_scoped_acquire at the beginning of my C++ callbacks (which in turn call the python callbacks), but then I get a deadlock because the lock is acquired again in the python callback it seems.
Yes, this is not for the sake of performance but because I have an already threaded API |
The deadlock is probably happening because of invocation from a function called itself from Python via a pybind-registered function: such a function holds the gil already and needs a |
@jcelerier would be useful to see minimal code example. In my project running python code in different C++ threads works nicely with appropriately used |
I had deadlocks when C++ threads were calling Python code when they attempted to aquire the GIL using |
Agreed. This is hard to judge or debug without reproducer. Thanks, @yesint! Closing. Please reopen if this is still an issue, and you want to further discuss with some concrete code. |
The documentation quickly mentions that threading is possible using "Python threads".
But given a C++ library that has its own threads, is it possible to interoperate with pybind11 ? For instance a network library has callbacks that execute in their own threads: how safe is it to pass a python function in place of such a callback ?
The text was updated successfully, but these errors were encountered: