Skip to content

Commit 04273ad

Browse files
[3.14] gh-134939: Add the concurrent.interpreters Module (gh-135414)
PEP-734 has been accepted (for 3.14). (FTR, I'm opposed to putting this under the concurrent package, but doing so is the SC condition under which the module can land in 3.14.) (cherry picked from commit 6214373, AKA gh-133958)
1 parent 8cb7d9a commit 04273ad

28 files changed

+389
-85
lines changed

.github/CODEOWNERS

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,13 @@ Doc/howto/clinic.rst @erlend-aasland
281281
# Subinterpreters
282282
**/*interpreteridobject.* @ericsnowcurrently
283283
**/*crossinterp* @ericsnowcurrently
284-
Lib/test/support/interpreters/ @ericsnowcurrently
285284
Modules/_interp*module.c @ericsnowcurrently
285+
Lib/test/test__interp*.py @ericsnowcurrently
286+
Lib/concurrent/interpreters/ @ericsnowcurrently
287+
Lib/test/support/channels.py @ericsnowcurrently
288+
Doc/library/concurrent.interpreters.rst @ericsnowcurrently
286289
Lib/test/test_interpreters/ @ericsnowcurrently
290+
Lib/concurrent/futures/interpreter.py @ericsnowcurrently
287291

288292
# Android
289293
**/*Android* @mhsmith @freakboy3742

Doc/library/concurrency.rst

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

Doc/library/concurrent.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The :mod:`!concurrent` package
22
==============================
33

4-
Currently, there is only one module in this package:
4+
This package contains the following modules:
55

66
* :mod:`concurrent.futures` -- Launching parallel tasks
7+
* :mod:`concurrent.interpreters` -- Multiple interpreters in the same process

Doc/library/python.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ overview:
2727
inspect.rst
2828
annotationlib.rst
2929
site.rst
30+
31+
.. seealso::
32+
33+
* See the :mod:`concurrent.interpreters` module, which similarly
34+
exposes core runtime functionality.

Doc/whatsnew/3.14.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ and improvements in user-friendliness and correctness.
8383
.. PEP-sized items next.
8484
8585
* :ref:`PEP 649 and 749: deferred evaluation of annotations <whatsnew314-pep649>`
86+
* :ref:`PEP 734: Multiple Interpreters in the Stdlib <whatsnew314-pep734>`
8687
* :ref:`PEP 741: Python Configuration C API <whatsnew314-pep741>`
8788
* :ref:`PEP 750: Template strings <whatsnew314-pep750>`
8889
* :ref:`PEP 758: Allow except and except* expressions without parentheses <whatsnew314-pep758>`
@@ -123,6 +124,101 @@ of Python. See :ref:`below <whatsnew314-refcount>` for details.
123124
New features
124125
============
125126

127+
.. _whatsnew314-pep734:
128+
129+
PEP 734: Multiple Interpreters in the Stdlib
130+
--------------------------------------------
131+
132+
The CPython runtime supports running multiple copies of Python in the
133+
same process simultaneously and has done so for over 20 years.
134+
Each of these separate copies is called an "interpreter".
135+
However, the feature had been available only through the C-API.
136+
137+
That limitation is removed in the 3.14 release,
138+
with the new :mod:`concurrent.interpreters` module.
139+
140+
There are at least two notable reasons why using multiple interpreters
141+
is worth considering:
142+
143+
* they support a new (to Python), human-friendly concurrency model
144+
* true multi-core parallelism
145+
146+
For some use cases, concurrency in software enables efficiency and
147+
can simplify software, at a high level. At the same time, implementing
148+
and maintaining all but the simplest concurrency is often a struggle
149+
for the human brain. That especially applies to plain threads
150+
(for example, :mod:`threading`), where all memory is shared between all threads.
151+
152+
With multiple isolated interpreters, you can take advantage of a class
153+
of concurrency models, like CSP or the actor model, that have found
154+
success in other programming languages, like Smalltalk, Erlang,
155+
Haskell, and Go. Think of multiple interpreters like threads
156+
but with opt-in sharing.
157+
158+
Regarding multi-core parallelism: as of the 3.12 release, interpreters
159+
are now sufficiently isolated from one another to be used in parallel.
160+
(See :pep:`684`.) This unlocks a variety of CPU-intensive use cases
161+
for Python that were limited by the :term:`GIL`.
162+
163+
Using multiple interpreters is similar in many ways to
164+
:mod:`multiprocessing`, in that they both provide isolated logical
165+
"processes" that can run in parallel, with no sharing by default.
166+
However, when using multiple interpreters, an application will use
167+
fewer system resources and will operate more efficiently (since it
168+
stays within the same process). Think of multiple interpreters as
169+
having the isolation of processes with the efficiency of threads.
170+
171+
.. XXX Add an example or two.
172+
.. XXX Link to the not-yet-added HOWTO doc.
173+
174+
While the feature has been around for decades, multiple interpreters
175+
have not been used widely, due to low awareness and the lack of a stdlib
176+
module. Consequently, they currently have several notable limitations,
177+
which will improve significantly now that the feature is finally
178+
going mainstream.
179+
180+
Current limitations:
181+
182+
* starting each interpreter has not been optimized yet
183+
* each interpreter uses more memory than necessary
184+
(we will be working next on extensive internal sharing between
185+
interpreters)
186+
* there aren't many options *yet* for truly sharing objects or other
187+
data between interpreters (other than :type:`memoryview`)
188+
* many extension modules on PyPI are not compatible with multiple
189+
interpreters yet (stdlib extension modules *are* compatible)
190+
* the approach to writing applications that use multiple isolated
191+
interpreters is mostly unfamiliar to Python users, for now
192+
193+
The impact of these limitations will depend on future CPython
194+
improvements, how interpreters are used, and what the community solves
195+
through PyPI packages. Depending on the use case, the limitations may
196+
not have much impact, so try it out!
197+
198+
Furthermore, future CPython releases will reduce or eliminate overhead
199+
and provide utilities that are less appropriate on PyPI. In the
200+
meantime, most of the limitations can also be addressed through
201+
extension modules, meaning PyPI packages can fill any gap for 3.14, and
202+
even back to 3.12 where interpreters were finally properly isolated and
203+
stopped sharing the :term:`GIL`. Likewise, we expect to slowly see
204+
libraries on PyPI for high-level abstractions on top of interpreters.
205+
206+
Regarding extension modules, work is in progress to update some PyPI
207+
projects, as well as tools like Cython, pybind11, nanobind, and PyO3.
208+
The steps for isolating an extension module are found at
209+
:ref:`isolating-extensions-howto`. Isolating a module has a lot of
210+
overlap with what is required to support
211+
:ref:`free-threading <whatsnew314-free-threaded-cpython>`,
212+
so the ongoing work in the community in that area will help accelerate
213+
support for multiple interpreters.
214+
215+
Also added in 3.14: :ref:`concurrent.futures.InterpreterPoolExecutor
216+
<whatsnew314-concurrent-futures-interp-pool>`.
217+
218+
.. seealso::
219+
:pep:`734`.
220+
221+
126222
.. _whatsnew314-pep750:
127223

128224
PEP 750: Template strings
@@ -1109,6 +1205,8 @@ calendar
11091205
concurrent.futures
11101206
------------------
11111207

1208+
.. _whatsnew314-concurrent-futures-interp-pool:
1209+
11121210
* Add :class:`~concurrent.futures.InterpreterPoolExecutor`,
11131211
which exposes "subinterpreters" (multiple Python interpreters in the
11141212
same process) to Python code. This is separate from the proposed API

Lib/concurrent/futures/interpreter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def run(self, task):
167167
except _interpqueues.QueueError:
168168
continue
169169
except ModuleNotFoundError:
170-
# interpreters.queues doesn't exist, which means
170+
# interpreters._queues doesn't exist, which means
171171
# QueueEmpty doesn't. Act as though it does.
172172
continue
173173
else:

0 commit comments

Comments
 (0)