@@ -247,22 +247,22 @@ global data. All the details can be found in the CPython documentation.
247
247
248
248
.. _subinterp :
249
249
250
- Sub-interpreter support
251
- =======================
250
+ Embedding Sub-interpreters
251
+ ==========================
252
252
253
253
A sub-interpreter is a separate interpreter instance which provides a
254
254
separate, isolated interpreter environment within the same process as the main
255
255
interpreter. Sub-interpreters are created and managed with a separate API from
256
256
the main interpreter. Beginning in Python 3.12, sub-interpreters each have
257
257
their own Global Interpreter Lock (GIL), which means that running a
258
258
sub-interpreter in a separate thread from the main interpreter can achieve true
259
- concurrency.
259
+ concurrency.
260
260
261
261
pybind11's sub-interpreter API can be found in ``pybind11/subinterpreter.h ``.
262
262
263
- pybind11 :class: `subinterpreter ` instances can be safely moved and shared between
264
- threads as needed. However, managing multiple threads and the lifetimes of multiple
265
- interpreters and their GILs can be challenging.
263
+ pybind11 :class: `subinterpreter ` instances can be safely moved and shared between
264
+ threads as needed. However, managing multiple threads and the lifetimes of multiple
265
+ interpreters and their GILs can be challenging.
266
266
Proceed with caution (and lots of testing)!
267
267
268
268
The main interpreter must be initialized before creating a sub-interpreter, and
@@ -307,7 +307,7 @@ sub-interpreters.
307
307
:class: `gil_scoped_release ` and :class: `gil_scoped_acquire ` can be used to
308
308
manage the GIL of a sub-interpreter just as they do for the main interpreter.
309
309
They both manage the GIL of the currently active interpreter, without the
310
- programmer having to do anything special or different. There is one important
310
+ programmer having to do anything special or different. There is one important
311
311
caveat:
312
312
313
313
.. note ::
@@ -319,7 +319,7 @@ caveat:
319
319
320
320
Each sub-interpreter will import a separate copy of each ``PYBIND11_EMBEDDED_MODULE ``
321
321
when those modules specify a ``multiple_interpreters `` tag. If a module does not
322
- specify a ``multiple_interpreters `` tag, then Python will report an ``ImportError ``
322
+ specify a ``multiple_interpreters `` tag, then Python will report an ``ImportError ``
323
323
if it is imported in a sub-interpreter.
324
324
325
325
Here is an example showing how to create and activate sub-interpreters:
@@ -393,8 +393,8 @@ it when it goes out of scope.
393
393
394
394
Best Practices for sub-interpreter safety:
395
395
396
- - Avoid moving or disarming RAII objects managing GIL and sub-interpreter lifetimes. Doing so can
397
- lead to confusion about lifetimes. (For example, accidentally extending a
396
+ - Avoid moving or disarming RAII objects managing GIL and sub-interpreter lifetimes. Doing so can
397
+ lead to confusion about lifetimes. (For example, accidentally extending a
398
398
:class: `subinterpreter_scoped_activate ` past the lifetime of it's :class: `subinterpreter `.)
399
399
400
400
- Never share Python objects across different interpreters.
@@ -410,9 +410,11 @@ Best Practices for sub-interpreter safety:
410
410
resulting Python object when the wrong interpreter was active.
411
411
412
412
- While sub-interpreters each have their own GIL, there can now be multiple independent GILs in one
413
- program you need to consider the possibility of deadlocks caused by multiple GILs and/or the
413
+ program you need to consider the possibility of deadlocks caused by multiple GILs and/or the
414
414
interactions of the GIL(s) and your C++ code's own locking.
415
415
416
416
- When using multiple threads to run independent sub-interpreters, the independent GILs allow
417
- concurrent calls from different interpreters into the same C++ code from different threads.
417
+ concurrent calls from different interpreters into the same C++ code from different threads.
418
418
So you must still consider the thread safety of your C++ code.
419
+
420
+ - Familiarize yourself with :ref: `misc_concurrency `.
0 commit comments