|
| 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 <[email protected]> |
| 8 | +.. sectionauthor:: Eric Snow <[email protected]> |
| 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". |
0 commit comments