|
core-adapters 0.1.1
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
|
The GIL: release it around C++ work, take it back to call Python, and a Python callable safe from any thread. More...
#include <nanobind/nanobind.h>#include <functional>#include <type_traits>#include <utility>Go to the source code of this file.
Classes | |
| class | snicholls::python::python_function< R(A...)> |
python_function for R(A...): takes the GIL, calls, converts the result to R. More... | |
Typedefs | |
| using | snicholls::python::release_gil = ::nanobind::call_guard<::nanobind::gil_scoped_release > |
| nanobind's call guard that releases the GIL for the duration of a bound call. | |
Functions | |
| template<class F > | |
| decltype(auto) | snicholls::python::without_gil (F &&f) |
Runs f() with the GIL released and returns its result. | |
| template<class F > | |
| decltype(auto) | snicholls::python::with_gil (F &&f) |
Runs f() holding the GIL (taking it if this thread does not hold it) and returns its result. | |
The GIL: release it around C++ work, take it back to call Python, and a Python callable safe from any thread.
snicholls::python::release_gil - nanobind's call guard, as a name: .def("f", &f, release_gil()) releases the GIL for the whole call, after the arguments are converted and before the result is converted back. Use it when every argument converts to a C++ value (numbers, std::span of a NumPy array, strings); never when the function touches a Python object.snicholls::python::without_gil(f) - inside a binding body: convert what needs the GIL first, then run f with the GIL released and return its result.snicholls::python::with_gil(f) - the reverse, from C++ code running without the GIL (a worker thread, or inside without_gil), to call into Python.snicholls::python::python_function<R(A...)> wraps a Python callable as a C++ callable that takes the GIL for each call (and for its own copy and destruction), so it can be handed to C++ that may run on any thread. Each call then pays for the lock: for a cooperative game evaluated 2^20 times, tabulate in Python once and pass the table instead (see snicholls_core.game).
gil_scoped_release / gil_scoped_acquire / call_guard, which these name.errors.hpp: a C++ exception thrown with the GIL released is translated after nanobind takes it back.without_gil(f) and with_gil(f) are exception-neutral: an exception from f propagates after the GIL state is restored.python_function holds a strong reference; copying and destroying take the GIL, so it may be copied and destroyed on any thread. Calling it from a thread that already holds the GIL is fine (re-entrant).nanobind::python_error, which nanobind turns back into the same Python exception when it reaches the binding. A result that does not convert to R is nanobind::type_error (Python TypeError). R must be default-constructible.python_function call adds the Python call itself. Measured as part of the call overhead in BENCHMARKS.md (tabular Shapley).scripts/demos/gil_demo.sh.snicholls::core_nanobind, inside a module built with nanobind_add_module (nanobind v3).nanobind::object inside without_gil - not even to copy or destroy it. Convert first.python_function called on a C++ thread can throw (nanobind::python_error); no exception may leave a thread, so catch it there, carry it back as a std::exception_ptr, and rethrow it on the calling thread after the join, with the GIL held again (tests/nanobind/gil_test.cpp, call_from_threads).release_gil with an argument that is a Python object (nanobind::object, nanobind::callable) is a crash waiting to happen: the object is destroyed after the call, with the GIL held again, but used during it without.Definition in file gil.hpp.