6
6
7
7
.. versionadded :: 3,7
8
8
9
- :ref: `_sub-interpreter-support `
10
-
11
- threading
12
-
13
9
--------------
14
10
15
11
This 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.
18
13
.. XXX The :mod:`interpreters` module provides an easier to use and
19
14
higher-level API built on top of this module.
20
15
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
+
21
31
This module is optional. It is provided by Python implementations which
22
32
support multiple interpreters.
23
-
24
33
.. XXX For systems lacking the :mod:`_interpreters` module, the
25
34
:mod:`_dummy_interpreters` module is available. It duplicates this
26
35
module's interface and can be used as a drop-in replacement.
@@ -42,9 +51,10 @@ It defines the following functions:
42
51
.. function :: run_string(id, command)
43
52
44
53
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.
48
58
49
59
Any value returned from the code is thrown away, similar to what
50
60
threads do. If the code results in an exception then that exception
@@ -62,9 +72,3 @@ It defines the following functions:
62
72
merged into the execution namespace before execution. Note that
63
73
this allows objects to leak between interpreters, which may not
64
74
be desirable.
65
-
66
-
67
- **Caveats: **
68
-
69
- * ...
70
-
0 commit comments