简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM