pybind11gil

Pybind11: Release GIL by default


I have a C++ library with many functions exported to Python using PyBind11. I am sure that these functions are thread-safe and would like to maximize the performance of multi-threading in Python. Normally we have to release GIL like:

PYBIND11_MODULE(MyModule, m) {
    m.def("foo", &foo, py::call_guard<py::gil_scoped_release>());
    m.def("bar", &bar, py::call_guard<py::gil_scoped_release>());
    ...
}

for all functions, including my class methods.

Is there a way to set GIL-release as library/application default so that I don't have to write py::call_guard<...> all the time and possibly gain some performance gain since I am sure there is no hidden function on the code path holding the GIL?


Solution

  • You can write macros to replace def or try to patch pybind11 yourself, but there's no official way I'm aware of.