Starter point: PEP 554 -- Multiple Interpreters in the Stdlib.
The goal is to support running multiple Python interpreters in parallel with
one lock per interpreter (no more "Global Interpreter Lock", but one
"Interpreter Lock" per interpreter). An interpreter would only be able to run
one Python thread holding the interpreter lock at the same time, but multiple
Python threads which released the interpreter lock (ex: to call a system call
like read()) can be run in parallel.
To maximize performances, shared states between interpreters must be minimized. Each share state must be carefully protected by a lock, which prevent to run code in parallel.
During Python 3.7 and 3.8 dev cycle, Eric Snow moved scattered core global
variables into a _PyRuntimeState structure which has a single global and shared
instance: _PyRuntime.
Most functions access directly to _PyRuntime, directly or indirectly:
PyThreadState *tstate = _PyThreadState_GET();access implicitly_PyRuntime.PyThreadState *tstate = _PyRuntimeState_GetThreadState(&_PyRuntime);gets access explicitly_PyRuntime. Getruntime->gilstate.tstate_current.
_PyRuntimeState fields:
cevalexitfuncs,nexitfuncsfinalizinggcgilstateinterpretersmain_threadopen_code_hook,open_code_userdata,audit_hook_headpre_initialized,core_initialized,initializedpreconfigxidregistry
- Move
_PyRuntimeState.gilstatetoPyInterpreterState:- Remove
_PyRuntimeState_GetThreadState() - Update
_PyThreadState_GET()
- Remove
- Move most
_PyRuntimeStatefields intoPyInterpreterState - Pass the "context" to private C functions: the context can be
_PyRuntime, a field of_PyRuntime, the Python thread state (tstate), etc.
- Functions of public C API must not be modified at this stage to add new "context" parameters. Only the internal C API can be modified.
- Get the current Python thread:
_PyRuntimeState_GetThreadState(&_PyRuntime). WIP:gilstatemust move toPyInterpreterState - Get the current interpreter:
tstate->interp.
PyInterpreterStatemoved to the internal C API_PyRuntimeStatestructure and_PyRuntimevariable created