msg148604 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2011-11-29 20:45 |
The documentation for Event.wait says: This method returns the internal flag on exit, so it will always return True except if a timeout is given and the operation times out. In fact, however, if the thread that sets the flag immediately clears it, wait will return False. Antoine looking at the code says that this appears to be intentional, and that would make sense since originally wait returned no value. My use case is one thread waiting on another to complete a work loop. Normally the worker thread goes to sleep after clearing the flag, but sometimes it immediately starts a new work loop. In either case I want the monitoring loop to take an action when the work loop completes, and raise an error if the wait times out. It looked to me like Event.wait would work in the scenario. |
|
|
msg148606 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2011-11-29 20:50 |
Thinking about it, if the flag's state if the wait does not expire is not guaranteed to be True, then what I really need is some way to know, when the wait call completes, whether or not it terminated because of a timeout or not. I can always query the value of the flag separately if its value can in any case be changed by another thread. I am perhaps using the wrong tool, I'll have to look at the docs further. |
|
|
msg148608 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2011-11-29 20:55 |
Ok, so digging a bit, the feature dates back to . Interestingly, the OP in that issue had already envisioned that race condition and had precisely proposed the feature to avoid it: “Note that in the above case, the event could be cleared between the return from x.wait and the execution of x.isSet (in another thread), and so this would operate as though x.wait had just timed out”. So I'm changing my mind and saying it seems desireable to return True if the wait succeeded before the timeout's end, even though the flag may have been reset in the meantime. In 3.2, Condition.wait also returns a boolean, meaning the check in Event.wait can be atomic. |
|
|
msg148610 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2011-11-29 21:00 |
Changing the title and component to reflect Antoine's new understanding :) |
|
|
msg148641 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2011-11-30 07:44 |
> So I'm changing my mind and saying it seems desireable to return True if the wait succeeded before the timeout's end, even though the flag may have been reset in the meantime. Agreed. What matters is that the event has been signaled, not that it's currently signaled. |
|
|
msg148984 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2011-12-07 18:45 |
Here's a patch. |
|
|
msg149018 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2011-12-08 04:29 |
On Wed, 07 Dec 2011 18:45:27 +0000, <report@bugs.python.org> wrote: > _______________________________________ > diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst > --- a/Doc/library/threading.rst > +++ b/Doc/library/threading.rst > @@ -804,8 +804,9 @@ > floating point number specifying a timeout for the operation in seconds > (or fractions thereof). > > - This method returns the internal flag on exit, so it will always return > - ``True`` except if a timeout is given and the operation times out. > + This method returns true if and only if the internal flag has been set to > + true by another thread, so it will always return ``True`` except if a > + timeout is given and the operation times out. This seems to imply that if the current thread previously set the event, the wait will return False, which is contradicted by the 'so' statement (which appears to be correct). --David |
|
|
msg149019 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2011-12-08 07:39 |
> This seems to imply that if the current thread previously set the event, > the wait will return False, which is contradicted by the 'so' statement > (which appears to be correct). You're probably referring to the "another thread" part... A suggestion ? I see you're a native speaker :-) |
|
|
msg149130 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2011-12-09 22:03 |
"set to True, either before the wait call or after the wait starts" |
|
|
msg149150 - (view) |
Author: Charles-François Natali (neologix) *  |
Date: 2011-12-10 12:55 |
Thanks, here's a patch updated accordingly. |
|
|
msg149160 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2011-12-10 14:23 |
LGTM. |
|
|
msg150809 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2012-01-07 17:27 |
New changeset eb39d862a250 by Charles-François Natali in branch '3.2': Issue #13502: threading: Fix a race condition in Event.wait() that made it http://hg.python.org/cpython/rev/eb39d862a250 New changeset 0fe63bb20e74 by Charles-François Natali in branch 'default': Issue #13502: threading: Fix a race condition in Event.wait() that made it http://hg.python.org/cpython/rev/0fe63bb20e74 |
|
|