Skip to content

Commit 1e44f98

Browse files
committed
Add some docs cross references
1 parent b6fcab6 commit 1e44f98

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

docs/advanced/embedding.rst

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

223222
Interpreter 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

255254
A sub-interpreter is a separate interpreter instance which provides a
256255
separate, isolated interpreter environment within the same process as the main
257256
interpreter. Sub-interpreters are created and managed with a separate API from
258257
the main interpreter. Beginning in Python 3.12, sub-interpreters each have
259258
their own Global Interpreter Lock (GIL), which means that running a
260259
sub-interpreter in a separate thread from the main interpreter can achieve true
261-
concurrency.
260+
concurrency.
262261

263262
pybind11'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.
268267
Proceed with caution (and lots of testing)!
269268

270269
The 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
310309
manage the GIL of a sub-interpreter just as they do for the main interpreter.
311310
They 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
313312
caveat:
314313

315314
.. note::
@@ -321,7 +320,7 @@ caveat:
321320

322321
Each sub-interpreter will import a separate copy of each ``PYBIND11_EMBEDDED_MODULE``
323322
when 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``
325324
if it is imported in a sub-interpreter.
326325

327326
Here is an example showing how to create and activate sub-interpreters:
@@ -395,8 +394,8 @@ it when it goes out of scope.
395394

396395
Best 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`.

docs/advanced/misc.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ following checklist.
155155
within pybind11 that will throw exceptions on certain GIL handling errors
156156
(reference counting operations).
157157

158+
.. _misc_free_threading:
159+
158160
Free-threading support
159161
==================================================================
160162

@@ -178,6 +180,8 @@ your code is thread safe. Modules must still be built against the Python free-t
178180
enable free-threading, even if they specify this tag. Adding this tag does not break
179181
compatibility with non-free-threaded Python.
180182

183+
.. _misc_subinterp:
184+
181185
Sub-interpreter support
182186
==================================================================
183187

@@ -224,6 +228,8 @@ You can explicitly disable sub-interpreter support in your module by using the
224228
:func:`multiple_interpreters::not_supported()` tag. This is the default behavior if you do not
225229
specify a multiple_interpreters tag.
226230

231+
.. _misc_concurrency:
232+
227233
Concurrency and Parallelism in Python with pybind11
228234
===================================================
229235

0 commit comments

Comments
 (0)