@@ -216,8 +216,7 @@ naturally:
216216:func: `py::mod_gil_not_used() `,
217217:func: `py::multiple_interpreters::per_interpreter_gil() `, and
218218:func: `py::multiple_interpreters::shared_gil() ` tags just like ``PYBIND11_MODULE ``.
219- See the multiple interpreters and free-threading sections of the pybind11 docs for
220- more information.
219+ See :ref: `misc_subinterp ` and :ref: `misc_free_threading ` for more information.
221220
222221
223222Interpreter lifetime
@@ -249,22 +248,22 @@ global data. All the details can be found in the CPython documentation.
249248
250249.. _subinterp :
251250
252- Sub-interpreter support
253- =======================
251+ Embedding Sub-interpreters
252+ ==========================
254253
255254A sub-interpreter is a separate interpreter instance which provides a
256255separate, isolated interpreter environment within the same process as the main
257256interpreter. Sub-interpreters are created and managed with a separate API from
258257the main interpreter. Beginning in Python 3.12, sub-interpreters each have
259258their own Global Interpreter Lock (GIL), which means that running a
260259sub-interpreter in a separate thread from the main interpreter can achieve true
261- concurrency.
260+ concurrency.
262261
263262pybind11's sub-interpreter API can be found in ``pybind11/subinterpreter.h ``.
264263
265- pybind11 :class: `subinterpreter ` instances can be safely moved and shared between
266- threads as needed. However, managing multiple threads and the lifetimes of multiple
267- interpreters and their GILs can be challenging.
264+ pybind11 :class: `subinterpreter ` instances can be safely moved and shared between
265+ threads as needed. However, managing multiple threads and the lifetimes of multiple
266+ interpreters and their GILs can be challenging.
268267Proceed with caution (and lots of testing)!
269268
270269The main interpreter must be initialized before creating a sub-interpreter, and
@@ -309,7 +308,7 @@ sub-interpreters.
309308:class: `gil_scoped_release ` and :class: `gil_scoped_acquire ` can be used to
310309manage the GIL of a sub-interpreter just as they do for the main interpreter.
311310They both manage the GIL of the currently active interpreter, without the
312- programmer having to do anything special or different. There is one important
311+ programmer having to do anything special or different. There is one important
313312caveat:
314313
315314.. note ::
@@ -321,7 +320,7 @@ caveat:
321320
322321Each sub-interpreter will import a separate copy of each ``PYBIND11_EMBEDDED_MODULE ``
323322when those modules specify a ``multiple_interpreters `` tag. If a module does not
324- specify a ``multiple_interpreters `` tag, then Python will report an ``ImportError ``
323+ specify a ``multiple_interpreters `` tag, then Python will report an ``ImportError ``
325324if it is imported in a sub-interpreter.
326325
327326Here is an example showing how to create and activate sub-interpreters:
@@ -395,8 +394,8 @@ it when it goes out of scope.
395394
396395Best Practices for sub-interpreter safety:
397396
398- - Avoid moving or disarming RAII objects managing GIL and sub-interpreter lifetimes. Doing so can
399- lead to confusion about lifetimes. (For example, accidentally extending a
397+ - Avoid moving or disarming RAII objects managing GIL and sub-interpreter lifetimes. Doing so can
398+ lead to confusion about lifetimes. (For example, accidentally extending a
400399 :class: `subinterpreter_scoped_activate ` past the lifetime of it's :class: `subinterpreter `.)
401400
402401- Never share Python objects across different interpreters.
@@ -412,9 +411,11 @@ Best Practices for sub-interpreter safety:
412411 resulting Python object when the wrong interpreter was active.
413412
414413- While sub-interpreters each have their own GIL, there can now be multiple independent GILs in one
415- program you need to consider the possibility of deadlocks caused by multiple GILs and/or the
414+ program you need to consider the possibility of deadlocks caused by multiple GILs and/or the
416415 interactions of the GIL(s) and your C++ code's own locking.
417416
418417- When using multiple threads to run independent sub-interpreters, the independent GILs allow
419- concurrent calls from different interpreters into the same C++ code from different threads.
418+ concurrent calls from different interpreters into the same C++ code from different threads.
420419 So you must still consider the thread safety of your C++ code.
420+
421+ - Familiarize yourself with :ref: `misc_concurrency `.
0 commit comments