-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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
Changes from 1 commit
e3aa50b
88e47fb
ce3505b
0a5d0e4
fe86353
382cff2
5b6eb17
226e75c
eb7f7eb
491a642
fb3e215
cdfa1e7
fe1c1ae
399042f
907bb8c
e4cbc66
c0b637e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| :mod:`!interpreters` --- Multiple Interpreters in the Same Process | ||
| ================================================================== | ||
|
|
||
| .. module:: 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/interpreters/__init__.py` | ||
|
|
||
| -------------- | ||
|
|
||
|
|
||
| Introduction | ||
| ------------ | ||
|
|
||
| The :mod:`!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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This deserves more elaboration, or a link, given it is the first mention of isolation & in the 'key details' block.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a little explanation. We can elaborate in a follow-up PR if needed. |
||
| * no implicit threads | ||
| * not all PyPI packages support use in multiple interpreters yet | ||
|
|
||
| .. XXX Are there other relevant details to list? | ||
|
|
||
|
|
||
| API Summary | ||
| ----------- | ||
|
|
||
| +----------------------------------+----------------------------------------------+ | ||
| | signature | description | | ||
| +==================================+==============================================+ | ||
| | ``list_all() -> [Interpreter]`` | Get all existing interpreters. | | ||
| +----------------------------------+----------------------------------------------+ | ||
| | ``get_current() -> Interpreter`` | Get the currently running interpreter. | | ||
| +----------------------------------+----------------------------------------------+ | ||
| | ``get_main() -> Interpreter`` | Get the main interpreter. | | ||
| +----------------------------------+----------------------------------------------+ | ||
| | ``create() -> Interpreter`` | Initialize a new (idle) Python interpreter. | | ||
| +----------------------------------+----------------------------------------------+ | ||
|
|
||
| | | ||
|
ezio-melotti marked this conversation as resolved.
Outdated
|
||
|
|
||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | signature | description | | ||
| +===================================================+===================================================+ | ||
| | ``class Interpreter`` | A single interpreter. | | ||
|
ezio-melotti marked this conversation as resolved.
Outdated
|
||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.id`` | The interpreter's ID (read-only). | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.whence`` | Where the interpreter came from (read-only). | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.is_running() -> bool`` | Is the interpreter currently executing code | | ||
| | | in its :mod:`!__main__` module? | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.close()`` | Finalize and destroy the interpreter. | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.prepare_main(ns=None, **kwargs)`` | Bind "shareable" objects in :mod:`!__main__`. | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.exec(src_str, /, dedent=True)`` | | Run the given source code in the interpreter | | ||
| | | | (in the current thread). | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.call(callable, /, *args, **kwargs)`` | | Run the given function in the interpreter | | ||
| | | | (in the current thread). | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
| | ``.call_in_thread(callable, /, *args, **kwargs)`` | | Run the given function in the interpreter | | ||
| | | | (in a new thread). | | ||
| +---------------------------------------------------+---------------------------------------------------+ | ||
|
ezio-melotti marked this conversation as resolved.
Outdated
|
||
|
|
||
| Exceptions: | ||
|
|
||
| +--------------------------+------------------+---------------------------------------------------+ | ||
| | class | base class | description | | ||
| +==========================+==================+===================================================+ | ||
| | InterpreterError | Exception | An interpreter-related error happened. | | ||
| +--------------------------+------------------+---------------------------------------------------+ | ||
| | InterpreterNotFoundError | InterpreterError | The targeted interpreter no longer exists. | | ||
| +--------------------------+------------------+---------------------------------------------------+ | ||
| | ExecutionFailed | InterpreterError | The running code raised an uncaught exception. | | ||
| +--------------------------+------------------+---------------------------------------------------+ | ||
| | NotShareableError | TypeError | The object cannot be sent to another interpreter. | | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to add some markup to these exceptions.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I'll be changing this table to the correct directive as soon as I can.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| +--------------------------+------------------+---------------------------------------------------+ | ||
|
|
||
| .. XXX Document the ExecutionFailed attrs. | ||
|
|
||
|
|
||
| .. XXX Add API summary for communicating between interpreters. | ||
|
|
||
|
|
||
| .. _interp-examples: | ||
|
|
||
| Basic Usage | ||
| ----------- | ||
|
|
||
| Creating an interpreter and running code in it: | ||
|
|
||
| :: | ||
|
AA-Turner marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 Describe module functions in more detail. | ||
|
|
||
|
|
||
| .. XXX Describe module types in more detail. | ||
|
|
||
|
|
||
| .. XXX Explain about object "sharing". | ||
Uh oh!
There was an error while loading. Please reload this page.