-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
(Mostly) Stop Special-casing the Main Interpreter #109857
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It might be useful to think about this hypothetical future API: Runtime/interpreter init/teardown for use in “plugins” that embed Python without any support from the enclosing “application”.
(If the “application” uses |
I like Petr's proposal, at least the way I'm reading it. Making So an embedder could create lightweight interpreters (i.e. no imports) and totally ignore search paths and all that stuff.2 That would be a great benefit and open up a lot of possibilities. So yeah, making the first interpreter into just another interpreter would be great. My only concern would be how it interacts with Footnotes |
Yeah, making |
(I'm going to write down some thoughts here, but I should probably open a DPO thread.) FWIW, I've also been thinking a lot about the distinct phases of init/fini and how making the boundaries actually structural in the code might be helpful. There's some similarity with PEP 432.
...and the finalization phases are mostly the mirror of that. Naturally there would be various sorts of config wedged in there too. Note that I'm not suggesting that level of granularity necessarily. |
PEP 587 added a half-baken private provisional API: https://docs.python.org/dev/c-api/init_config.html#multi-phase-initialization-private-provisional-api |
I abandoned my PR #108577 which adds PyInterpreterState_IsMain(). I'm no longer sure if this function is the right approach to handle sub-interpreters. |
Feature or enhancement
We make a bunch of accommodations for the "main" (initial) interpreter, especially during initialization and finalization. This adds complexity to the runtime. We should consider making the main interpreter (mostly) not special. At most we need to specifically special-case relative to the main thread.
The main thread of a Python program uses the initial thread state of the main interpreter. That thread is special for the following reasons, at least for the Python executable (and for some embedding cases):
Thus the initial thread state of the main interpreter plays a critical role. The code reflects this, especially in Python/pylifecycle.c. However, a lot of the special treatment of the main interpreter is unnecessary. 1
The main interpreter is the one created during runtime initialization and used during initialization. This is relevant for any non-immortal, non-static objects that we store on
_PyRuntimeState
, which we should strenuously avoid doing anyway. It is likely also relevant for embedders.There are also a couple of places where the "main" interpreter needs to be factored in (which we currently isn't), but doesn't necessarily need to be addressed with this issue:
os.fork()
should update the main thread state and the main interpreter (or only be allowed in the main thread of the main interpreter)?CC @markshannon, @vstinner, @ncoghlan, @zooba
Footnotes
FYI, we also optimize the code somewhat using the fact that the initial thread state of the main interpreter is likely to be the active one (e.g. that thread state and interpreter state are statically allocated and initialized). However, I'm not sure we need to change much about this. ↩
The text was updated successfully, but these errors were encountered: