66
77.. versionadded :: 3,7
88
9- :ref: `_sub-interpreter-support `
10-
11- threading
12-
139--------------
1410
1511This module provides low-level primitives for working with multiple
16- Python interpreters in the same process.
17-
12+ Python interpreters in the same runtime in the current process.
1813.. XXX The :mod:`interpreters` module provides an easier to use and
1914 higher-level API built on top of this module.
2015
16+ More information about (sub)interpreters is found at
17+ :ref: `_sub-interpreter-support `, including what data is shared between
18+ interpreters and what is unique. Note particularly that interpreters
19+ aren't inherently threaded, even though they track and manage Python
20+ threads. To run code in an interpreter in a different OS thread, call
21+ :func: `run_string ` in a function that you run in a new Python thread.
22+ For example::
23+
24+ id = _interpreters.create()
25+ def f():
26+ _interpreters.run_string(id, 'print("in a thread")')
27+
28+ t = threading.Thread(target=f)
29+ t.start()
30+
2131This module is optional. It is provided by Python implementations which
2232support multiple interpreters.
23-
2433.. XXX For systems lacking the :mod:`_interpreters` module, the
2534 :mod:`_dummy_interpreters` module is available. It duplicates this
2635 module's interface and can be used as a drop-in replacement.
@@ -42,9 +51,10 @@ It defines the following functions:
4251.. function :: run_string(id, command)
4352
4453 A wrapper around :c:func: `PyRun_SimpleString ` which runs the provided
45- Python program using the identified interpreter. Providing an
46- invalid or unknown ID results in a RuntimeError, likewise if the main
47- interpreter or any other running interpreter is used.
54+ Python program in the main thread of the identified interpreter.
55+ Providing an invalid or unknown ID results in a RuntimeError,
56+ likewise if the main interpreter or any other running interpreter
57+ is used.
4858
4959 Any value returned from the code is thrown away, similar to what
5060 threads do. If the code results in an exception then that exception
@@ -62,9 +72,3 @@ It defines the following functions:
6272 merged into the execution namespace before execution. Note that
6373 this allows objects to leak between interpreters, which may not
6474 be desirable.
65-
66-
67- **Caveats: **
68-
69- * ...
70-
0 commit comments