Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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
base:main
Are you sure you want to change the base?
Changes from1 commit
96e1e78e5e980e2fcaeddaea82784bb1e2fd1ab4021c2d40fade2ce340f50e70c3105b6028349d7a7cf0fb1394472e46fb675246d22e3ee0f7661da28f02181b79755b75c38035101a0ddae34c652abd3File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Co-authored-by: neonene <53406459+neonene@users.noreply.github.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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-sensitive | ||
| situations, where they shouldn't have access to each other's data. | ||
| 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 | ||
| @@ -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 | ||
| 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 | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. exampledefnon_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 MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
| @@ -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 anduses fewer | ||
| system resources. | ||
| Each interpreter has its own :mod:`!__main__` module, its own | ||
| @@ -375,7 +375,7 @@ the same rules as functions:: | ||
| Mutable State is not Shared | ||
| --------------------------- | ||
| 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 | ||
| @@ -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 | ||
| :mod:`!__main__` module will be in exactly the state in which you | ||
| left it:: | ||
| @@ -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 tracebackgets printed and, rather than a failure code, | ||
| an :class:`ExecutionFailed` exception is raised:: | ||
| from concurrent import interpreters | ||
Uh oh!
There was an error while loading.Please reload this page.