|
| 1 | +:mod:`!interpreters` --- Multiple Interpreters in the Same Process |
| 2 | +================================================================== |
| 3 | + |
| 4 | +.. module:: interpreters |
| 5 | + :synopsis: Multiple Interpreters in the Same Process |
| 6 | + |
| 7 | +.. moduleauthor:: Eric Snow <[email protected]> |
| 8 | +.. sectionauthor:: Eric Snow <[email protected]> |
| 9 | + |
| 10 | +.. versionadded:: 3.14 |
| 11 | + |
| 12 | +**Source code:** :source:`Lib/interpreters/__init__.py` |
| 13 | + |
| 14 | +-------------- |
| 15 | + |
| 16 | + |
| 17 | +Introduction |
| 18 | +------------ |
| 19 | + |
| 20 | +The :mod:`!interpreters` module constructs higher-level interfaces |
| 21 | +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 | +
|
| 54 | +API Summary |
| 55 | +----------- |
| 56 | + |
| 57 | ++----------------------------------+----------------------------------------------+ |
| 58 | +| signature | description | |
| 59 | ++==================================+==============================================+ |
| 60 | +| ``list_all() -> [Interpreter]`` | Get all existing interpreters. | |
| 61 | ++----------------------------------+----------------------------------------------+ |
| 62 | +| ``get_current() -> Interpreter`` | Get the currently running interpreter. | |
| 63 | ++----------------------------------+----------------------------------------------+ |
| 64 | +| ``get_main() -> Interpreter`` | Get the main interpreter. | |
| 65 | ++----------------------------------+----------------------------------------------+ |
| 66 | +| ``create() -> Interpreter`` | Initialize a new (idle) Python interpreter. | |
| 67 | ++----------------------------------+----------------------------------------------+ |
| 68 | + |
| 69 | +| |
| 70 | +
|
| 71 | ++---------------------------------------------------+---------------------------------------------------+ |
| 72 | +| signature | description | |
| 73 | ++===================================================+===================================================+ |
| 74 | +| ``class Interpreter`` | A single interpreter. | |
| 75 | ++---------------------------------------------------+---------------------------------------------------+ |
| 76 | +| ``.id`` | The interpreter's ID (read-only). | |
| 77 | ++---------------------------------------------------+---------------------------------------------------+ |
| 78 | +| ``.whence`` | Where the interpreter came from (read-only). | |
| 79 | ++---------------------------------------------------+---------------------------------------------------+ |
| 80 | +| ``.is_running() -> bool`` | Is the interpreter currently executing code | |
| 81 | +| | in its :mod:`!__main__` module? | |
| 82 | ++---------------------------------------------------+---------------------------------------------------+ |
| 83 | +| ``.close()`` | Finalize and destroy the interpreter. | |
| 84 | ++---------------------------------------------------+---------------------------------------------------+ |
| 85 | +| ``.prepare_main(ns=None, **kwargs)`` | Bind "shareable" objects in :mod:`!__main__`. | |
| 86 | ++---------------------------------------------------+---------------------------------------------------+ |
| 87 | +| ``.exec(src_str, /, dedent=True)`` | | Run the given source code in the interpreter | |
| 88 | +| | | (in the current thread). | |
| 89 | ++---------------------------------------------------+---------------------------------------------------+ |
| 90 | +| ``.call(callable, /, *args, **kwargs)`` | | Run the given function in the interpreter | |
| 91 | +| | | (in the current thread). | |
| 92 | ++---------------------------------------------------+---------------------------------------------------+ |
| 93 | +| ``.call_in_thread(callable, /, *args, **kwargs)`` | | Run the given function in the interpreter | |
| 94 | +| | | (in a new thread). | |
| 95 | ++---------------------------------------------------+---------------------------------------------------+ |
| 96 | + |
| 97 | +Exceptions: |
| 98 | + |
| 99 | ++--------------------------+------------------+---------------------------------------------------+ |
| 100 | +| class | base class | description | |
| 101 | ++==========================+==================+===================================================+ |
| 102 | +| InterpreterError | Exception | An interpreter-related error happened. | |
| 103 | ++--------------------------+------------------+---------------------------------------------------+ |
| 104 | +| InterpreterNotFoundError | InterpreterError | The targeted interpreter no longer exists. | |
| 105 | ++--------------------------+------------------+---------------------------------------------------+ |
| 106 | +| ExecutionFailed | InterpreterError | The running code raised an uncaught exception. | |
| 107 | ++--------------------------+------------------+---------------------------------------------------+ |
| 108 | +| NotShareableError | TypeError | The object cannot be sent to another interpreter. | |
| 109 | ++--------------------------+------------------+---------------------------------------------------+ |
| 110 | + |
| 111 | +.. XXX Document the ExecutionFailed attrs. |
| 112 | +
|
| 113 | +
|
| 114 | +.. XXX Add API summary for communicating between interpreters. |
| 115 | +
|
| 116 | +
|
| 117 | +.. _interp-examples: |
| 118 | + |
| 119 | +Basic Usage |
| 120 | +----------- |
| 121 | + |
| 122 | +Creating an interpreter and running code in it: |
| 123 | + |
| 124 | +:: |
| 125 | + |
| 126 | + import interpreters |
| 127 | + |
| 128 | + interp = interpreters.create() |
| 129 | + |
| 130 | + # Run in the current OS thread. |
| 131 | + |
| 132 | + interp.exec('print("spam!")') |
| 133 | + |
| 134 | + interp.exec("""if True: |
| 135 | + print('spam!') |
| 136 | + """) |
| 137 | + |
| 138 | + from textwrap import dedent |
| 139 | + interp.exec(dedent(""" |
| 140 | + print('spam!') |
| 141 | + """)) |
| 142 | + |
| 143 | + def run(): |
| 144 | + print('spam!') |
| 145 | + |
| 146 | + interp.call(run) |
| 147 | + |
| 148 | + # Run in new OS thread. |
| 149 | + |
| 150 | + t = interp.call_in_thread(run) |
| 151 | + t.join() |
| 152 | + |
| 153 | + |
| 154 | +.. XXX Describe module functions in more detail. |
| 155 | +
|
| 156 | +
|
| 157 | +.. XXX Describe module types in more detail. |
| 158 | +
|
| 159 | +
|
| 160 | +.. XXX Explain about object "sharing". |
0 commit comments