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
Clarify about Interpreter.exec().
  • Loading branch information
@ericsnowcurrently
ericsnowcurrently committedJul 3, 2025
commit675246d2e75f3762fba50c6695907eaef368f536

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

65 changes: 49 additions & 16 deletionsDoc/howto/multiple-interpreters.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -133,23 +133,12 @@ builtin :func:`exec` using that interpreter::

(See :meth:`Interpreter.exec`.)

Calling a simple function in an interpreter works the same way::
Calls are also supported.
See :ref:`the next section <interp-tutorial-calls>`.

from concurrent import interpreters

def script():
print('spam!')

if __name__ == '__main__':
interp = interpreters.create()
interp.call(script)
# prints: spam!

(See :meth:`Interpreter.call`.)

When it runs, the code is executed using the interpreter's
:mod:`!__main__` module, just like a Python process normally does when
invoked from the command-line::
When :meth:`Interpreter.exec` runs, the code is executed using the
interpreter's :mod:`!__main__` module, just like a Python process
normally does when invoked from the command-line::

from concurrent import interpreters

Expand DownExpand Up@@ -206,6 +195,50 @@ It's also fairly easy to simulate the other forms of the Python CLI::
That's more or less what the ``python`` executable is doing for each
of those cases.

You can also exec any function that doesn't take any arguments, nor
returns anything. Closures are not allowed but other nested functions
are. It works the same as if you had passed the script corresponding
to the function's body::

from concurrent import interpreters

def script():
print('spam!')

def get_script():
def nested():
print('eggs!')
return nested

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

interp.exec(script)
# prints: spam!

script2 = get_script()
interp.exec(script2)
# prints: eggs!

Any referenced globals are resolved relative to the interpreter's
:mod:`!__main__` module, just like happens for scripts, rather than
the original function's module::

from concurrent import interpreters

def script():
print(__name__)

if __name__ == '__main__':
interp = interpreters.create()
interp.exec(script)
# prints: __main__

One practical difference is that with a script function you get syntax
highlighting in your editor. With script text you probably don't.

.. _interp-tutorial-calls:

Calling a Function in an Interpreter
------------------------------------

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp