@@ -39,15 +39,42 @@ The ``PYBIND11_MAKE_OPAQUE`` macro does *not* require the above workarounds.
39
39
Global Interpreter Lock (GIL)
40
40
=============================
41
41
42
- When calling a C++ function from Python, the GIL is always held.
42
+ The Python C API dictates that the Global Interpreter Lock (GIL) must always
43
+ be held by the current thread to safely access Python objects. As a result,
44
+ when Python calls into C++ via pybind11 the GIL must be held, and pybind11
45
+ will never implicitly release the GIL.
46
+
47
+ .. code-block :: cpp
48
+
49
+ void my_function() {
50
+ /* GIL is held when this function is called from Python */
51
+ }
52
+
53
+ PYBIND11_MODULE(example, m) {
54
+ m.def("my_function", &my_function);
55
+ }
56
+
57
+ pybind11 will ensure that the GIL is held when it knows that it is calling
58
+ Python code. For example, if a Python callback is passed to C++ code via
59
+ ``std::function ``, when C++ code calls the function the built-in wrapper
60
+ will acquire the GIL before calling the Python callback. Similarly, the
61
+ ``PYBIND11_OVERRIDE `` family of macros will acquire the GIL before calling
62
+ back into Python.
63
+
64
+ When writing C++ code that is called from other C++ code, if that code accesses
65
+ Python state, it must explicitly acquire and release the GIL.
66
+
43
67
The classes :class: `gil_scoped_release ` and :class: `gil_scoped_acquire ` can be
44
68
used to acquire and release the global interpreter lock in the body of a C++
45
69
function call. In this way, long-running C++ code can be parallelized using
46
- multiple Python threads. Taking :ref: `overriding_virtuals ` as an example, this
70
+ multiple Python threads, **but great care must be taken ** when any
71
+ :class: `gil_scoped_release ` appear: if there is any way that the C++ code
72
+ can access Python objects, :class: `gil_scoped_acquire ` should be used to
73
+ reacquire the GIL. Taking :ref: `overriding_virtuals ` as an example, this
47
74
could be realized as follows (important changes highlighted):
48
75
49
76
.. code-block :: cpp
50
- :emphasize-lines: 8,9 ,31,32
77
+ :emphasize-lines: 8,30 ,31
51
78
52
79
class PyAnimal : public Animal {
53
80
public:
@@ -56,9 +83,7 @@ could be realized as follows (important changes highlighted):
56
83
57
84
/* Trampoline (need one for each virtual function) */
58
85
std::string go(int n_times) {
59
- /* Acquire GIL before calling Python code */
60
- py::gil_scoped_acquire acquire;
61
-
86
+ /* PYBIND11_OVERRIDE_PURE will acquire the GIL before accessing Python state */
62
87
PYBIND11_OVERRIDE_PURE(
63
88
std::string, /* Return type */
64
89
Animal, /* Parent class */
@@ -78,7 +103,8 @@ could be realized as follows (important changes highlighted):
78
103
.def(py::init<>());
79
104
80
105
m.def("call_go", [](Animal *animal) -> std::string {
81
- /* Release GIL before calling into (potentially long-running) C++ code */
106
+ // GIL is held when called from Python code. Release GIL before
107
+ // calling into (potentially long-running) C++ code
82
108
py::gil_scoped_release release;
83
109
return call_go(animal);
84
110
});
0 commit comments