-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Should asyncio.{Lock,Semaphore}.locked()
return True when there are waiters?
#97028
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
I assume you meant @njsmith not |
What makes this a tough call? Like, this method doesn't seem that important in the grand scheme of things, but being more accurate is generally nicer than being less accurate, right? |
Just that in the past it's always worked like this. I came up with a restaurant metaphor for semaphores (surely not unique) and it's the difference between "there is currently a table with no dining guests" and "we can seat you right away". Also the code for acquire() calls locked() in a few places so we'd have to change those to |
That metaphor gets the point. We can be more accurate with three methods (names can vary): |
But was it really changed? The old code also checked for value==0 in locked() and in release() it also bumped the value. So the case where there are waiters who have been buzzed but who haven't yet claimed their table would be the same, wouldn't it? This issue is not about making the change, it's about deciding what locked() should return. (While I have your attention, did you see #93222 (comment)?) |
As someone pointed out, the old code might be buggy on that. It meant to be
I guess
Yeah, there I dropped a reply. |
Okay, um, let me summarize what I am seeing.
Have I got that right? Since in the distant past it meant can_seat(), and that is clearly the more useful semantics, we should change it to that, right? |
Yeah you got everything, except for "the distant past" which I don't know. The above PR changed the semantics of |
@gvanrossum Is this fixed by #97549? |
Yes, it is, thanks for the reminder. |
Currently when an
asyncio
Lock
orSemaphore
has been recently vacated but still has waiters queued up, thelocked()
method may returnFalse
, making prospective acquirers believe that if they callacquire()
they will immediately proceed, without blocking.In a threading system with pre-emptive scheduling (like Python's
threading
module, which uses OS threads) this makes sense, becauselocked()
never makes any guarantee: code likemay block in the
acquire()
call because another thread may be executing similar code and there's a race.But in
asyncio
there's no race -- there is no pre-emptive task switching and everything associated with a particular event loop runs in a single OS thread. So we could make it so thatlocked()
checks if the queue is empty and returnsTrue
only if it is. But should we?CC: @kumaraditya303 @cykerway @njs.
The text was updated successfully, but these errors were encountered: