Skip to content

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

Open
ericsnowcurrently wants to merge 23 commits into
python:mainfrom
ericsnowcurrently:multiple-interpreters-howto
Open

gh-134939: Add a Multiple Interpreters Howto Doc#136143
ericsnowcurrently wants to merge 23 commits into
python:mainfrom
ericsnowcurrently:multiple-interpreters-howto

Conversation

@ericsnowcurrently

@ericsnowcurrentlyericsnowcurrently commented Jun 30, 2025

Copy link
Copy Markdown
Member

@ericsnowcurrently
ericsnowcurrently marked this pull request as ready for review June 30, 2025 23:00
Comment threadDoc/howto/multiple-interpreters.rst Outdated
Comment threadDoc/howto/multiple-interpreters.rst Outdated
Comment threadDoc/howto/multiple-interpreters.rst Outdated
Comment threadDoc/howto/multiple-interpreters.rst Outdated
Comment threadDoc/howto/multiple-interpreters.rst Outdated
Comment threadDoc/howto/multiple-interpreters.rst Outdated
Comment threadDoc/howto/multiple-interpreters.rst Outdated
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
Copy Markdown
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 the InterpreterPoolExecutor case, though.

Copy link
Copy Markdown
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.


if __name__ == '__main__':
interp = interpreters.create()
t = interp.call_in_thread(script)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Off-topic, but would it be possible to add the daemon parameter to the helper method?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

I want to keep the helper super simple. If someone wants a daemon thread then they can do it manually without much effort.

@ericsnowcurrently

Copy link
Copy Markdown
MemberAuthor

Any objections to merging this?

example = dedent(os.linesep.join(current))
expected = ''.join(f'{l}{os.linesep}' for l in expected)
examples[start] = (example, expected)
current = expected = start = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This line is unused:

Suggested change
current = expected = start = None

Comment on lines +66 to +67
def write_example(examplesdir, name, text):
filename = os.path.join(examplesdir, f'example-{name}.py')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
defwrite_example(examplesdir, name, text):
filename=os.path.join(examplesdir, f'example-{name}.py')
defwrite_example(examples_dir, name, text):
filename=os.path.join(examples_dir, f'example-{name}.py')

Comment on lines +83 to +84
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
capture_output=True,

if failed:
print(f'{len(failed)} failed: {",".join(failed)}')
else:
print(f'all succeeded')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
print(f'all succeeded')
print('all succeeded')

name: Run Ruff (lint) on Doc/
args: [--exit-non-zero-on-fix]
files: ^Doc/
exclude: ^Doc/howto/multiple-interpreters-run-examples.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's remove these exclusions and do the fixes?

Suggested change
exclude: ^Doc/howto/multiple-interpreters-run-examples.py

Comment on lines +976 to +977
Sharing Data
------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Sharing Data
------------
Sharing data
------------

Comment on lines +991 to +992
Using Queues
------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Using Queues
------------
Using queues
------------

Comment on lines +1033 to +1034
Initializing Globals for a Script
---------------------------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Initializing Globals for a Script
---------------------------------
Initializing globals for a script
---------------------------------

Comment on lines +1125 to +1126
Pipes, Sockets, etc.
--------------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Pipes, Sockets, etc.
--------------------
Pipes, sockets, and so on
-------------------------

Avoid Latin abbreviations: https://devguide.python.org/documentation/style-guide/#use-simple-language

Comment on lines +1272 to +1273
Tutorial: Miscellaneous
=======================

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Tutorial: Miscellaneous
=======================
Tutorial: miscellaneous
=======================

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label Apr 26, 2026
@serhiy-storchakaserhiy-storchaka added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 30, 2026
@github-actionsgithub-actionsBot removed the stale Stale PR or inactive for long period of time. label Jun 1, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core reviewdocsDocumentation in the Doc dirneeds backport to 3.14bugs and security fixesneeds backport to 3.15pre-release feature fixes, bugs and security fixesskip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

5 participants

@ericsnowcurrently@hugovk@neonene@serhiy-storchaka@nazeerali4325-commits