开发者

Efficient way to timeout waiting for a state change (variable change)?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜