Condition.wait(0.5) doesn't respect it's timeout

Piet van Oostrum piet at cs.uu.nl
Sat Apr 18 14:12:46 EDT 2009


>>>>> "Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> (GG) wrote:

>GG> If another thread has acquired the lock, cond.wait() doesn't return. Add
>GG> these lines at the end of your test and see:

>GG> sleep(2)
>GG> print "Main thread - cond.acquire()"
>GG> cond.acquire()
>GG> sleep(2)
>GG> print "Main thread - cond.release()"
>GG> cond.release()
>GG> sleep(2)
>GG> sys.exit()

>GG> The timeout is detected, but the wait method doesn't return, it's stuck at
>GG> the last line (trying to restore a saved RLock state).
>GG> I don't understand the logic behind that.

That is the semantics of a Condition. The cond.wait() must be done in a
critical section, i.e. when the thread has acquired the lock. The wait()
temporarily releases the lock so that another thread can try to make the
way free for the waiting thread and give a notify(). When the wait()
continues it must first re-acquire the lock to make sure it has
exclusive access to the critical section. If another thread still has
the lock it has to wait for the lock being released. This is also the
case when the wait continues because the timeout has expired.

>GG> Please file a bug report at http://bugs.python.org/

It is not a bug. I see the Hello World temporarily suspended and then
continuing. That's how it should be.
-- 
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 mailing list