no return value for threading.Condition.wait(timeout)?
Piet van Oostrum
piet at cs.uu.nl
Thu Jul 16 18:03:52 EDT 2009
>>>>> Gabriel Rossetti <gabriel.rossetti at arimaz.com> (GR) wrote:
>GR> Hello everyone,
>GR> I am using threading.Condition.wait(timeout) and was surprised to see that
>GR> there is no return value nor an exception when wait() is used w/ a timeout.
>GR> How am I supposed to know if it was notified or if it timed out?
Normally you wouldn't have to know. The logic of your program should be
such that you wait until a certain condition is satisfied. After each
wait you usually check that condition anyway, like:
The only exception could be if there is a 1-1 relation between a single
waiter and a single notifier. I that case you can usually use if instead
of while (but it does depend of the logic of the notifier).
So in case of using a timeout you can check the desired condition after
the wait and decide what to do. If the condition was not satisfied you
probably want to do something special, like error handling, or do
something else first and then retry the wait.
There could be situations where this doesn't work, however. For example
if you wait until a queue is not empty, and the notify is done when
something is inserted in the queue, another thread may sneak in while
you are waiting and snatch the inserted item just before your thread
continues after the wait. In that case you can't distinguish between a
timeout and a snatcher.
So it all depends on what your use case is.
(Java's wait doesn't return anything either.)
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org
More information about the Python-list