-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-134939: Add the interpreters Module #133958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ericsnowcurrently
wants to merge
17
commits into
python:main
Choose a base branch
from
ericsnowcurrently:pep-734-accepted
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e3aa50b
Move the interpreters module to the official stdlib.
ericsnowcurrently 88e47fb
Add a NEWS entry.
ericsnowcurrently ce3505b
Add a whatsnew entry.
ericsnowcurrently 0a5d0e4
Add initial docs.
ericsnowcurrently fe86353
Fix formatting and typos.
ericsnowcurrently 382cff2
Fix doc lint.
ericsnowcurrently 5b6eb17
Minor docs fixes.
ericsnowcurrently 226e75c
Clarify about isolation.
ericsnowcurrently eb7f7eb
Hide the channels module.
ericsnowcurrently 491a642
Fix a test.
ericsnowcurrently fb3e215
Make interpreters.queues private.
ericsnowcurrently cdfa1e7
interpreters -> concurrent.interpreters
ericsnowcurrently fe1c1ae
Update Doc/library/concurrent.interpreters.rst
ericsnowcurrently 399042f
Update Doc/library/concurrent.interpreters.rst
ericsnowcurrently 907bb8c
Fix the NEWS entry.
ericsnowcurrently e4cbc66
Drop the module __getattr__.
ericsnowcurrently c0b637e
Apply suggestions from code review.
ericsnowcurrently File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
The :mod:`!concurrent` package | ||
============================== | ||
|
||
Currently, there is only one module in this package: | ||
This package contains the following modules: | ||
|
||
* :mod:`concurrent.futures` -- Launching parallel tasks | ||
* :mod:`concurrent.interpreters` -- Multiple interpreters in the same process |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.