VisionSystemsInc / pyvxl

Python wrapper for vxl using pybind11
4 stars 5 forks source link

GIL Releasing #59

Open NoahRJohnson opened 5 years ago

NoahRJohnson commented 5 years ago

By default PyBind11 holds Python's global interpreter lock during C++ calls. We can explicitly release and then re-acquire the GIL around bound functions, allowing multiple Python threads to run PyVXL functions concurrently. This is a helpful feature in Terra DSM. As more and more functions are used by Terra DSM, we GIL scope more and more PyVXL functions. Are there drawbacks to this? I figured we could discuss this here. If the pros outweight the cons, should we just gil scope every PyVXL function by default?

One drawback is if in the future we were ever to call Python code from a bound C++ function, we would need the function to be careful about re-acquiring the GIL.

Relevant docs: https://pybind11.readthedocs.io/en/stable/advanced/misc.html#global-interpreter-lock-gil

decrispell commented 5 years ago

I had to add py::gil_scoped_release release; to some code today in order to make a python GUI that was calling pybind11-wrapped C++ more responsive. The project is unrelated to pyvxl itself, but just thought I'd add a data point.

All it took was that one line added on the C++ side and it did wonders for the responsiveness on the Python side.