Skip to content

docs: show nogil in most examples #5770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/advanced/cast/custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type is explicitly allowed.
} // namespace pybind11

// Bind the negate function
PYBIND11_MODULE(docs_advanced_cast_custom, m) { m.def("negate", user_space::negate); }
PYBIND11_MODULE(docs_advanced_cast_custom, m, py::mod_gil_not_used()) { m.def("negate", user_space::negate); }

.. note::

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/cast/functional.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ trivial to generate binding code for all of these functions.

#include <pybind11/functional.h>

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
m.def("func_arg", &func_arg);
m.def("func_ret", &func_ret);
m.def("func_cpp", &func_cpp);
Expand Down
6 changes: 3 additions & 3 deletions docs/advanced/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Normally, the binding code for these classes would look as follows:

.. code-block:: cpp

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
py::class_<Animal>(m, "Animal")
.def("go", &Animal::go);

Expand Down Expand Up @@ -112,7 +112,7 @@ The binding code also needs a few minor adaptations (highlighted):
.. code-block:: cpp
:emphasize-lines: 2,3

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
py::class_<Animal, PyAnimal /* <--- trampoline */, py::smart_holder>(m, "Animal")
.def(py::init<>())
.def("go", &Animal::go);
Expand Down Expand Up @@ -774,7 +774,7 @@ to Python.

#include <pybind11/operators.h>

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
py::class_<Vector2>(m, "Vector2")
.def(py::init<float, float>())
.def(py::self + py::self)
Expand Down
4 changes: 2 additions & 2 deletions docs/advanced/pycpp/numpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ expects the type followed by field names:
};

// ...
PYBIND11_MODULE(test, m) {
PYBIND11_MODULE(test, m, py::mod_gil_not_used()) {
// ...

PYBIND11_NUMPY_DTYPE(A, x, y);
Expand Down Expand Up @@ -351,7 +351,7 @@ simply using ``vectorize``).
return result;
}

PYBIND11_MODULE(test, m) {
PYBIND11_MODULE(test, m, py::mod_gil_not_used()) {
m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}

Expand Down
4 changes: 2 additions & 2 deletions docs/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ a file named :file:`example.cpp` with the following contents:
return i + j;
}

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
m.doc() = "pybind11 example plugin"; // optional module docstring

m.def("add", &add, "A function that adds two numbers");
Expand Down Expand Up @@ -288,7 +288,7 @@ converted using the function ``py::cast``.

.. code-block:: cpp

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
m.attr("the_answer") = 42;
py::object world = py::cast("World");
m.attr("what") = world;
Expand Down
2 changes: 1 addition & 1 deletion docs/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def generate_dummy_code_pybind11(nclasses=10):
result = "#include <pybind11/pybind11.h>\n\n"
result += "namespace py = pybind11;\n\n"
result += decl + "\n"
result += "PYBIND11_MODULE(example, m) {\n"
result += "PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {\n"
result += bindings
result += "}"
return result
Expand Down
2 changes: 1 addition & 1 deletion docs/benchmark.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Here is an example of the binding code for one class:
};
...

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
...
py::class_<cl034>(m, "cl034")
.def("fn_000", &cl034::fn_000)
Expand Down
4 changes: 2 additions & 2 deletions docs/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The binding code for ``Pet`` looks as follows:

namespace py = pybind11;

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &>())
.def("setName", &Pet::setName)
Expand Down Expand Up @@ -480,7 +480,7 @@ management. For example, ownership is inadvertently transferred here:
std::shared_ptr<Child> child;
};

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
py::class_<Child, std::shared_ptr<Child>>(m, "Child");

py::class_<Parent, std::shared_ptr<Parent>>(m, "Parent")
Expand Down
5 changes: 2 additions & 3 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ following example:
void init_ex2(py::module_ &);
/* ... */

PYBIND11_MODULE(example, m) {
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
init_ex1(m);
init_ex2(m);
/* ... */
Expand Down Expand Up @@ -235,8 +235,7 @@ been received, you must either explicitly interrupt execution by throwing

.. code-block:: cpp

PYBIND11_MODULE(example, m)
{
PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
m.def("long running_func", []()
{
for (;;) {
Expand Down
Loading