@@ -448,6 +448,14 @@ Expected output:
448
448
After Main, still within sub; Current Interpreter is 1
449
449
At end; Current Interpreter is 0
450
450
451
+ .. warning ::
452
+
453
+ In Python 3.12 sub-interpreters must be destroyed in the same OS thread
454
+ that created them. Failure to follow this rule may result in deadlocks
455
+ or crashes when destroying the sub-interpreter on the wrong thread.
456
+
457
+ This constraint is not present in Python 3.13+.
458
+
451
459
452
460
Best Practices for sub-interpreter safety
453
461
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -458,7 +466,8 @@ Best Practices for sub-interpreter safety
458
466
and :func: `error_already_set::what() ` acquires the GIL. So Python exceptions must
459
467
**never ** be allowed to propagate past the enclosing
460
468
:class: `subinterpreter_scoped_activate ` instance!
461
- (So your try/catch should be *just inside * the scope covered by the :class: `subinterpreter_scoped_activate `.)
469
+ (So your try/catch should be *just inside * the scope covered by the
470
+ :class: `subinterpreter_scoped_activate `.)
462
471
463
472
- Avoid global/static state whenever possible. Instead, keep state within each interpreter,
464
473
such as within the interpreter state dict, which can be accessed via
@@ -480,6 +489,7 @@ Best Practices for sub-interpreter safety
480
489
481
490
- When using multiple threads to run independent sub-interpreters, the independent GILs allow
482
491
concurrent calls from different interpreters into the same C++ code from different threads.
483
- So you must still consider the thread safety of your C++ code.
492
+ So you must still consider the thread safety of your C++ code. Remember, in Python 3.12
493
+ sub-interpreters must be destroyed on the same thread that they were created on.
484
494
485
495
- Familiarize yourself with :ref: `misc_concurrency `.
0 commit comments