1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
:mod:`!concurrent.interpreters` --- Multiple interpreters in the same process
=============================================================================
.. module:: concurrent.interpreters
:synopsis: Multiple interpreters in the same process
.. moduleauthor:: Eric Snow <ericsnowcurrently@gmail.com>
.. sectionauthor:: Eric Snow <ericsnowcurrently@gmail.com>
.. versionadded:: 3.14
**Source code:** :source:`Lib/concurrent/interpreters.py`
--------------
Introduction
------------
The :mod:`!concurrent.interpreters` module constructs higher-level
interfaces on top of the lower level :mod:`!_interpreters` module.
.. XXX Add references to the upcoming HOWTO docs in the seealso block.
.. seealso::
:ref:`isolating-extensions-howto`
how to update an extension module to support multiple interpreters
:pep:`554`
:pep:`734`
:pep:`684`
.. XXX Why do we disallow multiple interpreters on WASM?
.. include:: ../includes/wasm-notavail.rst
Key details
-----------
Before we dive into examples, there are a small number of details
to keep in mind about using multiple interpreters:
* isolated, by default
* no implicit threads
* not all PyPI packages support use in multiple interpreters yet
.. XXX Are there other relevant details to list?
In the context of multiple interpreters, "isolated" means that
different interpreters do not share any state. In practice, there is some
process-global data they all share, but that is managed by the runtime.
Reference
---------
This module defines the following functions:
.. function:: list_all()
Return a :class:`list` of :class:`Interpreter` objects,
one for each existing interpreter.
.. function:: get_current()
Return an :class:`Interpreter` object for the currently running
interpreter.
.. function:: get_main()
Return an :class:`Interpreter` object for the main interpreter.
.. function:: create()
Initialize a new (idle) Python interpreter
and return a :class:`Interpreter` object for it.
Interpreter objects
^^^^^^^^^^^^^^^^^^^
.. class:: Interpreter(id)
A single interpreter in the current process.
Generally, :class:`Interpreter` shouldn't be called directly.
Instead, use :func:`create` or one of the other module functions.
.. attribute:: id
(read-only)
The interpreter's ID.
.. attribute:: whence
(read-only)
A string describing where the interpreter came from.
.. method:: is_running()
Return ``True`` if the interpreter is currently executing code
in its :mod:`!__main__` module and ``False`` otherwise.
.. method:: close()
Finalize and destroy the interpreter.
.. method:: prepare_main(ns=None, **kwargs)
Bind "shareable" objects in the interpreter's
:mod:`!__main__` module.
.. method:: exec(code, /, dedent=True)
Run the given source code in the interpreter (in the current thread).
.. method:: call(callable, /, *args, **kwargs)
Return the result of calling running the given function in the
interpreter (in the current thread).
.. method:: call_in_thread(callable, /, *args, **kwargs)
Run the given function in the interpreter (in a new thread).
Exceptions
^^^^^^^^^^
.. exception:: InterpreterError
This exception, a subclass of :exc:`Exception`, is raised when
an interpreter-related error happens.
.. exception:: InterpreterNotFoundError
This exception, a subclass of :exc:`InterpreterError`, is raised when
the targeted interpreter no longer exists.
.. exception:: ExecutionFailed
This exception, a subclass of :exc:`InterpreterError`, is raised when
the running code raised an uncaught exception.
.. attribute:: excinfo
A basic snapshot of the exception raised in the other interpreter.
.. XXX Document the excinfoattrs?
.. exception:: NotShareableError
This exception, a subclass of :exc:`TypeError`, is raised when
an object cannot be sent to another interpreter.
.. XXX Add functions for communicating between interpreters.
Basic usage
-----------
Creating an interpreter and running code in it::
from concurrent import interpreters
interp = interpreters.create()
# Run in the current OS thread.
interp.exec('print("spam!")')
interp.exec("""if True:
print('spam!')
""")
from textwrap import dedent
interp.exec(dedent("""
print('spam!')
"""))
def run():
print('spam!')
interp.call(run)
# Run in new OS thread.
t = interp.call_in_thread(run)
t.join()
.. XXX Explain about object "sharing".
|