Multiple interpreters in a Python process — Python 3.14.7 documentation

docs.python.org

Tin mới

Thread-local storage support

Profiling and tracing

Python Module Index

3.14.7 Documentation

Python/C API reference manual

PyThreadState_Swap()

While in most uses, you will only embed a single Python interpreter, there are cases where you need to create several independent interpreters in the same process and perhaps even in the same thread. Sub-interpreters all

subprocess: Subprocess management.

If this is 0 then the runtime will not support forking the process in any thread where the sub-interpreter is currently active. Otherwise fork is unrestricted.

threading: Thread-based parallelism.

If this is 0 then the sub-interpreter’s threading module won’t create threads. Otherwise threads are allowed.

Py_mod_multiple_interpreters

If this is 0 then all extension modules may be imported, including legacy (single-phase init) modules, in any thread where the sub-interpreter is currently active. Otherwise only multi-phase init extension modules (see P

builtins: The module that provides the built-in namespace.

The given config controls the options with which the interpreter is initialized.

__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``.

The given config controls the options with which the interpreter is initialized.

sys: Access system-specific parameters and functions.

The given config controls the options with which the interpreter is initialized.

attached thread state

The given config controls the options with which the interpreter is initialized.

single-phase initialization

Note that this is different from what happens when an extension is imported after the interpreter has been completely re-initialized by calling Py_FinalizeEx() and Py_Initialize(); in that case, the extension’s initmodul

ctypes: A foreign function library for Python.

Special care should be taken to avoid sharing user-defined functions, methods, instances or classes between sub-interpreters, since import operations executed by such objects may affect the wrong (sub-)interpreter’s dict

_PyInterpreterFrame

The throwflag parameter is used by the throw() method of generators: if non-zero, handle the current exception.