| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
I think it does make sense to land this now after all. The code won't function as intended until the GIL is disabled by default, but it will be nice to have the module slot available while I work on gh-116738, so I can tag modules as they're ready. This is structured such that there shouldn't be any behavioral differences until the GIL is disabled by default, except when a --disable-gil build is run with -v, when it will print a single line for every non-free-threading-safe module that's loaded. |
Sorry, something went wrong.
|
I'll take a look on Monday. |
Sorry, something went wrong.
There was a problem hiding this comment.
mostly LGTM
I've left a few comments and I have some feedback on the relationship between Py_mod_multiple_interpreters and Py_mod_gil.
Sorry, something went wrong.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request. |
Sorry, something went wrong.
|
@ericsnowcurrently I don't think there should be any code changes as a result of our big sub-thread. Please let me know if that's wrong! Before this is ready for another review, I need to make some other changes. It looks like the consensus in Discord is that we should enable the GIL when calling a module init function, because we don't know before the call if it's a single-phase init module that needs the GIL. Then if it's a multi-phase init module that provides Py_GIL_NOT_USED, we can disable the GIL and continue, otherwise we leave the GIL on for the rest of the interpreter's lifetime. I'll make an issue soon to nail down how we handle single-phase init modules. The only nontrivial (but not too complicated) part of this will be adding a function to disable the GIL safely. It should also move the GIL-related logic up out of PyModule_FromDefAndSpec2(), which will be a nice side-effect. |
Sorry, something went wrong.
Yeah, I think you're right. |
Sorry, something went wrong.
More details: - Fix a race while enabling the GIL by checking if the GIL was enabled between a no-op call to `_PyEval_AcquireLock()` and the thread attaching, and trying again if it was. - Enable the GIL before running a module init function, since we can't know if it's a single-phase init module that doesn't support free-threading. Look at the state of the module after initialization to determine if it's safe to disable the GIL again. - Add `PyModule_SetGIL()`, which can be used by single-phase init modules to declare that they support running without the GIL. - Change `gil->enabled` from a simple on/off switch to a count of active requests to enable the GIL. This allows us to support multiple interleaved imports that each independently track whether the GIL should remain enabled. See the big comment in `pycore_ceval.h` for more details.
|
I have made the requested changes; please review again. This should be ready for review again. It grew in scope a bit, and now includes support for single-phase init modules (gh-117526) (some details are in the commit message). With the infrastructure needed to properly support multi-phase init modules, supporting single-phase init modules is just a few extra lines, but I can pull it out into a separate PR if anyone wants. One specific thing I'd like input on is the timing of the _PyImport_CheckGILForModule() call in PyModule_FromDefAndSpec2(): I put it before module creation to disable the GIL as soon as possible for modules that don't need the GIL. The downside of this is that if the module does need the GIL and a subsequent part of the import process fails, the GIL will remain enabled even though we didn't actually load the module. Maybe this should be rare enough that it's not worth worrying about. |
Sorry, something went wrong.
|
!buildbot nogil |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @colesbury for commit 77d1652 🤖 The command will test the builders whose names match following regular expression: nogil The builders matched are:
|
Sorry, something went wrong.
|
I found a few more modules that I missed during some testing, and will push a commit to add those shortly. |
Sorry, something went wrong.
|
!buildbot nogil |
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @swtaarrs for commit d1fe0cc 🤖 The command will test the builders whose names match following regular expression: nogil The builders matched are:
|
Sorry, something went wrong.
|
|
||
| .. versionadded:: 3.9 | ||
|
|
||
| .. c:function:: int PyModule_ExperimentalSetGIL(PyObject *module, void *gil) |
There was a problem hiding this comment.
Ehm, can this please be renamed to PyUnstable as suggested before? We have such naming schemes for a reason.
Sorry, something went wrong.
There was a problem hiding this comment.
Sorry, I'll get a PR ready. I went with this name because having both Unstable and Experimental in the name felt redundant, and in this comment, @ericsnowcurrently expressed a preference for PyModule_ExperimentalSetGIL() over PyUnstable_Module_SetGIL().
Does anyone else have strong feelings on whether it should be PyUnstable_Module_ExperimentalSetGIL() or PyUnstable_Module_SetGIL()?
Sorry, something went wrong.
Sorry, something went wrong.
This PR adds the ability to enable the GIL if it was disabled at interpreter startup, and modifies the multi-phase module initialization path to enable the GIL when loading a module, unless that module's spec includes a slot indicating it can run safely without the GIL. PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went with `Py_MOD_GIL_NOT_USED` for consistency with pythongh-104148. A warning will be issued up to once per interpreter for the first GIL-using module that is loaded. If `-v` is given, a shorter message will be printed to stderr every time a GIL-using module is loaded (including the first one that issues a warning).
| } | ||
| } | ||
| m->md_def = module; | ||
| #ifdef Py_GIL_DISABLE |
There was a problem hiding this comment.
Should be Py_GIL_DISABLED.
Sorry, something went wrong.
This means: "Do not re-enable the GIL when importing rl.readline." See python/cpython#116322 See python/cpython#116882
| Back | FazBrowse Home | New Git URL |
This PR adds the ability to enable the GIL if it was disabled at interpreter startup, and modifies the multi-phase module initialization path to enable the GIL when loading a module, unless that module's spec includes a slot indicating it can run safely without the GIL.
PEP 703 called the constant for the slot Py_mod_gil_not_used; I went with Py_MOD_GIL_NOT_USED for consistency with gh-104148.
A warning will be issued up to once per interpreter for the first GIL-using module that is loaded. If -v is given, a shorter message will be printed to stderr every time a GIL-using module is loaded (including the first one that issues a warning).
📚 Documentation preview 📚: https://cpython-previews--116882.org.readthedocs.build/