no return value for threading.Condition.wait(timeout)?
gabriel.rossetti at arimaz.com
Fri Jul 17 02:25:23 EDT 2009
Piet van Oostrum wrote:
>>>>>> 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:
> while (some_condition_satisfied):
> 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).
I have a 1-1 relation, I have a thread reading msgs from a network
socket and a method waiting for an answer to arrive (see my thread
"Threading.Condition problem"). I would like to be able to have a
timeout as to not block forever, so the idea is to check if I returned
because of a timeout or not.
> 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.)
More information about the Python-list