You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[subinterpreters] Meta issue: per-interpreter GIL #84692
To be able to run multiple (sub)interpreters in parallel, the unique global interpreter lock aka "GIL" should be replace with multiple GILs: one "GIL" per interpreter. The scope of such per-interpreter GIL would be a single interpreter.
The current CPython code base is not fully read to have one GIL per interpreter. TODO:
Make free lists per interpreters: tuple, dict, frame, etc.
Make Unicode interned strings per interpreter
Make Unicode latin1 single character string singletons per interpreter
None, True, False, ... singletons: make them per-interpreter (bpo-39511) or immortal (bpo-40255)
etc.
Until we can ensure that no Python object is shared between two interpreters, we might make PyObject.ob_refcnt, PyGC_Head (_gc_next and _gc_prev) and _dictkeysobject.dk_refcnt atomic.
Replace globals ("static" variables) with a module state, or design a new "per-interpreter" local storage similar to Thread Local Storage (TLS). There is already PyInterpreterState.dict which is convenient to use in "Python" code, but it's not convenient to use in "C" code (code currently using "static int ..." for example).
I'm not sure how to handle C extensions which are binding for a C library which has a state and so should not be used multiple times in parallel. Some C extensions use a "global lock" for that. The question is how to get
This issue is related to PEP-554 "Multiple Interpreters in the Stdlib", but not required by this PEP.
This issue is a tracker for sub-issues related to the goal "have one GIL per interpreter".
--
Some changes have a negative impact on "single threaded" Python application. Even if the overhead is low, one option to be able to move faster on this issue may be to add a new temporary configure option to have an opt-in build mode to better isolate subinterpreters. Examples:
disable pymalloc
atomic reference counters
disable free lists
That would be a temporary solution to "unblock" the development on this list. For the long term, free lists should be made per-interpreter, pymalloc should support multiple interpreters, no Python object must be shared by two interpreters, etc.
--
One idea to detect if a Python object is shared by two interpreters *in debug mode* would be to store a reference to the interpreter which created it, and then check if the current interpreter is the same. If not, fail with a Python Fatal Error.
--
During Python 3.9 development cycle, many states moved from the global _PyRuntimeState to per-interpreter PyInterpreterState:
And more code is now shared for the initialization and finalization of the main interpreter and subinterpreters (ex: see bpo-38858).
Subinterpreters builtins and sys are now really isolated from the main interpreter (bpo-38858).
--
Obviously, there are likely tons of other issues which are not known at this stage. Again, this issue is a placeholder to track them all. It may be more efficient to create one sub-issue per sub-task, rather than discussing all tasks at the same place.
Some changes have a negative impact on "single threaded" Python application. Even if the overhead is low, one option to be able to move faster on this issue may be to add a new temporary configure option to have an opt-in build mode to better isolate subinterpreters. (...)
I created bpo-40514: "Add --experimental-isolated-subinterpreters build option".
Hum, demo.py is not reliable for threads: the standard deviation is quite large. I rewrote it using pyperf to compute the average and the standard deviation.
PyStructSequence_InitType2() is not compatible with subinterpreters: it uses static types. Moreover, it allocates tp_members memory which is not released when the type is destroyed. But I'm not sure that the type is ever destroyed, since this API is designed for static types.
PyStructSequence_InitType2() is not compatible with subinterpreters: it uses static types. Moreover, it allocates tp_members memory which is not released when the type is destroyed. But I'm not sure that the type is ever destroyed, since this API is designed for static types.
IMO, I suggest to create a new function, PyStructSequence_FromModuleAndDesc(module, desc, flags) to create a heaptype and don't aloocates memory block for tp_members,something like 'PyType_FromModuleAndSpec()`.
I don't know there have any block issue to do this converting operation. But I can take a look.
@petr ping, Petr, do you have any better idea about this question :)
IMO, I suggest to create a new function, PyStructSequence_FromModuleAndDesc()
Please create a new issue. If possible, I would prefer to have a sub-issue for that, to keep this issue as a tracking issue for all issues related to subinterpreters.
PyStructSequence_NewType exists, and is the same as the proposed PyStructSequence_FromModuleAndDesc except it doesn't take the module (which isn't necessary: PyStructSequence_Desc has no way to define functionality that would need the module state).
Activity
vstinner commentedon May 5, 2020
To be able to run multiple (sub)interpreters in parallel, the unique global interpreter lock aka "GIL" should be replace with multiple GILs: one "GIL" per interpreter. The scope of such per-interpreter GIL would be a single interpreter.
The current CPython code base is not fully read to have one GIL per interpreter. TODO:
Until we can ensure that no Python object is shared between two interpreters, we might make PyObject.ob_refcnt, PyGC_Head (_gc_next and _gc_prev) and _dictkeysobject.dk_refcnt atomic.
C extension modules should be modified as well:
I'm not sure how to handle C extensions which are binding for a C library which has a state and so should not be used multiple times in parallel. Some C extensions use a "global lock" for that. The question is how to get
Most of these tasks are already tracked in Eric Snow's "Multi Core Python" project:
https://github.com/ericsnowcurrently/multi-core-python/issues
This issue is related to PEP-554 "Multiple Interpreters in the Stdlib", but not required by this PEP.
This issue is a tracker for sub-issues related to the goal "have one GIL per interpreter".
--
Some changes have a negative impact on "single threaded" Python application. Even if the overhead is low, one option to be able to move faster on this issue may be to add a new temporary configure option to have an opt-in build mode to better isolate subinterpreters. Examples:
That would be a temporary solution to "unblock" the development on this list. For the long term, free lists should be made per-interpreter, pymalloc should support multiple interpreters, no Python object must be shared by two interpreters, etc.
--
One idea to detect if a Python object is shared by two interpreters *in debug mode* would be to store a reference to the interpreter which created it, and then check if the current interpreter is the same. If not, fail with a Python Fatal Error.
--
During Python 3.9 development cycle, many states moved from the global _PyRuntimeState to per-interpreter PyInterpreterState:
Many corner cases related to daemon threads have also been fixed:
And more code is now shared for the initialization and finalization of the main interpreter and subinterpreters (ex: see bpo-38858).
Subinterpreters builtins and sys are now really isolated from the main interpreter (bpo-38858).
--
Obviously, there are likely tons of other issues which are not known at this stage. Again, this issue is a placeholder to track them all. It may be more efficient to create one sub-issue per sub-task, rather than discussing all tasks at the same place.
vstinner commentedon May 5, 2020
I created bpo-40513: "Move _PyRuntimeState.ceval to PyInterpreterState".
vstinner commentedon May 5, 2020
I created bpo-40514: "Add --experimental-isolated-subinterpreters build option".
vstinner commentedon May 5, 2020
I created bpo-40522: "Subinterpreters: get the current Python interpreter state from Thread Local Storage (autoTSSkey)".
vstinner commentedon May 5, 2020
Attached demo.py: benchmark to compare performance of sequential execution, threads and subinterpreters.
vstinner commentedon May 5, 2020
(oops, there was a typo in my script: threads and subinterpreters was the same benchmark)
vstinner commentedon May 5, 2020
Hum, demo.py is not reliable for threads: the standard deviation is quite large. I rewrote it using pyperf to compute the average and the standard deviation.
vstinner commentedon May 5, 2020
I updated demo-pyperf.py to also benchmark multiprocessing.
vstinner commentedon May 6, 2020
I created bpo-40533: "Subinterpreters: don't share Python objects between interpreters".
26 remaining items
vstinner commentedon Aug 18, 2021
PyStructSequence_InitType2() is not compatible with subinterpreters: it uses static types. Moreover, it allocates tp_members memory which is not released when the type is destroyed. But I'm not sure that the type is ever destroyed, since this API is designed for static types.
shihai1991 commentedon Sep 6, 2021
IMO, I suggest to create a new function, PyStructSequence_FromModuleAndDesc(module, desc, flags) to create a heaptype and don't aloocates memory block for tp_members,something like 'PyType_FromModuleAndSpec()`.
I don't know there have any block issue to do this converting operation. But I can take a look.
@petr ping, Petr, do you have any better idea about this question :)
vstinner commentedon Sep 6, 2021
Hai Shi:
Please create a new issue. If possible, I would prefer to have a sub-issue for that, to keep this issue as a tracking issue for all issues related to subinterpreters.
shihai1991 commentedon Sep 6, 2021
bpo-45113: [subinterpreters][C API] Add a new function to create PyStructSequence from Heap.
encukou commentedon Oct 12, 2021
PyStructSequence_NewType exists, and is the same as the proposed PyStructSequence_FromModuleAndDesc except it doesn't take the module (which isn't necessary: PyStructSequence_Desc has no way to define functionality that would need the module state).
vstinner commentedon Nov 3, 2022
Sadly, I don't have the bandwidth to work on this issue, so I just close it.
@ericsnowcurrently is now working on https://peps.python.org/pep-0684/ which is a little bit different.