-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Add sys._is_gil_enabled()
#117514
Comments
What do you expect developers to do with this value? Is it merely for reporting details about bugs? Or is there some programmatic reason you may want to know this? |
I'd expect likely just reporting. Similar to things like |
I think the diagnostic and bug reporting uses are most straight-forward: is the GIL really disabled? Especially because it looks like we will be temporarily enabling the GIL around imports. I think it might also be useful in a few cases for choosing reasonable defaults: i.e., does a thread-pool or multiprocessing backend make more sense as a default in some library (like PyTorch data loaders or joblib). |
I fear it won't be particularly useful for either of those cases with the current proposed (temporary?) semantics. Reporting feels like it ought to be "was the GIL enabled when X went wrong", while selecting defaults feels like "will the GIL be enabled under normal operation". Otherwise the semantics of "don't call this during import" are going to be very unhelpful, both to explain and to reveal internal behaviour details and require users to design around them. How about a CPython-specific (FWIW, I'd have argued |
That seems like a more confusing API to me. I don't think there's a sufficient need for What do you mean by "don't call this during import"? |
We are only planning to do the temporary GIL enabling around imports of C-API extensions, not Python modules so I don't think there's any reason not to call |
Mechanisms that don't exist in, say, IronPython, which is nonetheless free threaded, and given the choice between threads or processes you would likely choose threads for similar kinds of tasks that you would under free-threaded CPython. The |
I agree, and I think that |
It just feels a bit weird to suddenly introduce "GIL" as a Python-wide concept when previously it was only ever a CPython implementation detail (a public one, to be sure, but not a cross-implementation concern in any way). Especially since the plan is to eventually have everyone return Why is it so bad to make it a documented underscored function |
I prefer It's more the combination of |
It's also fair to just start with the |
I've updated the issue to |
I came here to ask for this feature, I needed it to determine which execution model would work best (multiprocessing or thread pools) at runtime and the In answer to the "why would anyone need this", I'd just spent an hour looking for this flag :-) |
I was aware of this scenario, which is why I suggested |
The function returns `True` or `False` depending on whether the GIL is currently enabled. In the default build, it always returns `True` because the GIL is always enabled.
The function returns `True` or `False` depending on whether the GIL is currently enabled. In the default build, it always returns `True` because the GIL is always enabled.
The function returns `True` or `False` depending on whether the GIL is currently enabled. In the default build, it always returns `True` because the GIL is always enabled.
The function returns `True` or `False` depending on whether the GIL is currently enabled. In the default build, it always returns `True` because the GIL is always enabled.
Feature or enhancement
I propose adding a new function to the
sys
module to check if the GIL is current enabled:The function would always return
True
in builds that do not support free-threading. In the free-threaded build, the the GIL might be dynamically enabled or disabled depending on imported modules.EDIT: Changed name to use underscore prefix
Linked PRs
sys._is_gil_enabled()
function #118514The text was updated successfully, but these errors were encountered: