@@ -256,17 +256,19 @@ interpreter. Sub-interpreters are created and managed with a separate API from
256256the main interpreter. Beginning in Python 3.12, sub-interpreters each have
257257their own Global Interpreter Lock (GIL), which means that running a
258258sub-interpreter in a separate thread from the main interpreter can achieve true
259- concurrency.
259+ concurrency.
260260
261- Managing multiple threads and the lifetimes of multiple interpreters and their
262- GILs can be challenging. Proceed with caution (and lots of testing)!
261+ pybind11's sub-interpreter API can be found in ``pybind11/subinterpreter.h ``.
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.
266+ Proceed with caution (and lots of testing)!
263267
264268The main interpreter must be initialized before creating a sub-interpreter, and
265269the main interpreter must outlive all sub-interpreters. Sub-interpreters are
266270managed through a different API than the main interpreter.
267271
268- The sub-interpreter API can be found in ``pybind11/subinterpreter.h ``.
269-
270272The :class: `subinterpreter ` class manages the lifetime of sub-interpreters.
271273Instances are movable, but not copyable. Default constructing this class does
272274*not * create a sub-interpreter (it creates an empty holder). To create a
@@ -391,6 +393,10 @@ it when it goes out of scope.
391393
392394Best Practices for sub-interpreter safety:
393395
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+ :class: `subinterpreter_scoped_activate ` past the lifetime of it's :class: `subinterpreter `.)
399+
394400- Never share Python objects across different interpreters.
395401
396402- Avoid global/static state whenever possible. Instead, keep state within each interpreter,
@@ -404,5 +410,9 @@ Best Practices for sub-interpreter safety:
404410 resulting Python object when the wrong interpreter was active.
405411
406412- While sub-interpreters each have their own GIL, there can now be multiple independent GILs in one
407- program, so your code needs to consider thread safety of within the C++ code, and the possibility
408- of deadlocks caused by multiple GILs and/or the interactions of the GIL(s) and C++'s own locking.
413+ program you need to consider the possibility of deadlocks caused by multiple GILs and/or the
414+ interactions of the GIL(s) and your C++ code's own locking.
415+
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.
418+ So you must still consider the thread safety of your C++ code.
0 commit comments