I'm sure this is somewhat common, so I am curious what are the accepted/efficient ways of doing this in Python.
Put simply, I am just busy-waiting for a variable to be updated. At the same time I need a timeout scheme, but I feel there must be a better way of doing this.
Currently I do something like this:
wait_start = time.time()
while state != NEW_STATE:
if time.time() - wait_start > timeout:
print "Timed out!"
# Do something
# Continuing on...
I obviously just can't sleep, because I need to know when the state has changed.
So what is an effi开发者_运维百科cient method of implementing a timeout for a state (variable) change?
Condition variables and events are often used for this type of thing. Both require cooperation from the side that's changing the variable.
精彩评论