Condition.wait(timeout) oddities

Floris Bruynooghe floris.bruynooghe at gmail.com
Mon May 23 06:53:05 EDT 2011


Hi all

I'm a little confused about the corner cases of Condition.wait() with a timeout parameter in the threading module.

When looking at the code the first thing that I don't quite get is that the timeout should never work as far as I understand it.  .wait() always needs to return while holding the lock, therefore it does an .acquire() on the lock in a finally clause.  Thus pretty much ignoring the timeout value.

The second issue is that while looking around for this I found two bug reports: http://bugs.python.org/issue1175933 and http://bugs.python.org/issue10218.  Both are proposing to add a return value indicating whether the .wait() timed out or not similar to the other .wait() methods in threading.  However the first was rejected after some (seemingly inconclusive) discussion.  While the latter had minimal discussion and and was accepted without reference to the earlier attempt.  Not sure if this was a process oversight or what, but it does leave the situation confusing.

But regardless I don't understand how the return value can be used currently: yes you did time out but you're still promised to hold the lock thanks to the .acquire() call on the lock in the finally block.

In my small brain I just can't figure out how Condition.wait() can both respect a timeout parameter and the promise to hold the lock on return.  It seems to me that the only way to handle the timeout is to raise an exception rather then return a value because when you get an exception you can break the promise of holding the lock.

But maybe I'm missing something important or obvious, so I'd be happy to be enlightened!

Regards
Floris



More information about the Python-list mailing list