Skip to content

Commit 0a5d0e4

Browse files
Add initial docs.
1 parent ce3505b commit 0a5d0e4

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

Doc/library/concurrency.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ multitasking). Here's an overview:
2323
queue.rst
2424
contextvars.rst
2525

26+
Also see the :mod:`interpreters` module.
27+
2628

2729
The following are support modules for some of the above services:
2830

Doc/library/interpreters.rst

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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".

Doc/library/python.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ overview:
1717
builtins.rst
1818
__main__.rst
1919
warnings.rst
20+
interpreters.rst
2021
dataclasses.rst
2122
contextlib.rst
2223
abc.rst

0 commit comments

Comments
 (0)