Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-134939: Add a Multiple Interpreters Howto Doc#136143

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ericsnowcurrently wants to merge23 commits intopython:main
base:main
Choose a base branch
Loading
fromericsnowcurrently:multiple-interpreters-howto
Open
Changes from1 commit
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
96e1e78
Add the multiple interpreters howto doc.
ericsnowcurrentlyJul 16, 2024
e5e980e
Drop extraneous changes.
ericsnowcurrentlyJun 19, 2025
2fcaedd
Finish howto doc, minus recipes.
ericsnowcurrentlyJun 20, 2025
aea8278
Add a TODO list.
ericsnowcurrentlyJun 26, 2025
4bb1e2f
Drop the execcomponents ref.
ericsnowcurrentlyJun 30, 2025
d1ab402
Fix a ref.
ericsnowcurrentlyJun 30, 2025
1c2d40f
Drop the examples section for now.
ericsnowcurrentlyJun 30, 2025
ade2ce3
Fix typos.
ericsnowcurrentlyJul 1, 2025
40f50e7
Add a misc section to the tutorial.
ericsnowcurrentlyJul 1, 2025
0c3105b
Clarify about prepare_main().
ericsnowcurrentlyJul 1, 2025
6028349
Fix a pseudo-ref.
ericsnowcurrentlyJul 1, 2025
d7a7cf0
Fix the examples.
ericsnowcurrentlyJul 1, 2025
fb13944
Tweak one example.
ericsnowcurrentlyJul 1, 2025
72e46fb
Add a ref.
ericsnowcurrentlyJul 2, 2025
675246d
Clarify about Interpreter.exec().
ericsnowcurrentlyJul 3, 2025
22e3ee0
Clarify about calling different kinds of function.
ericsnowcurrentlyJul 3, 2025
f7661da
Clarify a note.
ericsnowcurrentlyJul 3, 2025
28f0218
Add a caveat about -c and the REPL.
ericsnowcurrentlyJul 3, 2025
1b79755
Add a pro tip.
ericsnowcurrentlyJul 3, 2025
b75c380
Add run-examples.py and fix some of the examples.
ericsnowcurrentlySep 23, 2025
35101a0
Do not try reformatting run-examples.py.
ericsnowcurrentlySep 24, 2025
ddae34c
Fix some of the examples.
ericsnowcurrentlySep 24, 2025
652abd3
Do not lint run-examples.py.
ericsnowcurrentlySep 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Fix typos.
Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
  • Loading branch information
@ericsnowcurrently@neonene
ericsnowcurrently andneonene authoredJul 1, 2025
commitade2ce347112187b5b42bb706347764d28efdc37

Some comments aren't visible on the classic Files Changed page.

14 changes: 7 additions & 7 deletionsDoc/howto/multiple-interpreters.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,10 +61,10 @@ completely isolated from the others.
isolated from one another since there are few restrictions on memory
access within the same process. The Python runtime makes a best
effort at isolation but extension modules may easily violate that.
Therefore, do not use multiple interpreters in security-senstive
Therefore, do not use multiple interpreters in security-sensitive
situations, where they shouldn't have access to each other's data.

That isolation facilitates a concurrency model basedan independent
That isolation facilitates a concurrency model basedon independent
logical threads of execution, like CSP or the actor model.

Each actual thread in Python, even if you're only running in the main
Expand All@@ -86,7 +86,7 @@ There are some downsides and temporary limitations:
discipline about how the isolated components in your program interact
* not all PyPI extension modules support multiple interpreters yet
* the existing tools for passing data between interpreters safely
is still relatively inefficient and limited
are still relatively inefficient and limited
* actually *sharing* data safely is tricky (true for free-threading too)
* all necessary modules must be imported separately in each interpreter
* relatively slow startup time per interpreter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Some users might need a (foot)note that subinterpreters can sometimes improve the startup by importing nothing.

example
defnon_stateless():non_statelessif__name__=='__main__':importthreadingfromconcurrentimportinterpretersinterps= [interpreters.create()foriinrange(10)]threads= [threading.Thread(target=interp.call,args=(non_stateless,))forinterpininterps    ]forthreadinthreads:thread.start()forthreadinthreads:thread.join()

That seems not so beneficial for theInterpreterPoolExecutor case, though.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I've added a note, though further down in the discussion about calling functions.

neonene reacted with heart emoji
Expand All@@ -103,7 +103,7 @@ Tutorial: Basics
First of all, keep in mind that using multiple interpreters is like
using multiple processes. They are isolated and independent from each
other. The main difference is that multiple interpreters live in the
same process, which makes it all more efficient anduse fewer
same process, which makes it all more efficient anduses fewer
system resources.

Each interpreter has its own :mod:`!__main__` module, its own
Expand DownExpand Up@@ -375,7 +375,7 @@ the same rules as functions::
Mutable State is not Shared
---------------------------

Justbe be clear, the underlying data of very few mutable objects is
Justto be clear, the underlying data of very few mutable objects is
actually shared between interpreters. The notable exceptions are
:class:`Queue` and :class:`memoryview`, which we will explore in a
little while. In nearly every case, the raw data is copied in
Expand DownExpand Up@@ -428,7 +428,7 @@ the builtin :func:`exec`, it doesn't reset the namespace it uses
to run the code.

In the same way, running code in an interpreter does not reset that
interpreter.. The next time you run code in that interpreter, the
interpreter. The next time you run code in that interpreter, the
:mod:`!__main__` module will be in exactly the state in which you
left it::

Expand DownExpand Up@@ -535,7 +535,7 @@ and there's an unhandled exception. In that case, Python will print
the traceback and the process will exit with a failure code.

The behavior is very similar when code is run in an interpreter.
The tracebackget printed and, rather than a failure code,
The tracebackgets printed and, rather than a failure code,
an :class:`ExecutionFailed` exception is raised::

from concurrent import interpreters
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp