Is there an easy way in Python to wait until certain condition is true?

Posted by Checkers on Stack Overflow See other posts from Stack Overflow or by Checkers
Published on 2010-05-07T02:33:14Z Indexed on 2010/05/07 2:38 UTC
Read the original article Hit count: 219

Filed under:

I need to wait in a script until a certain number of conditions become true?

I know I can roll my own eventing using condition variables and friends, but I don't want to go through all the trouble of implementing it, since some object property changes come from external thread in a wrapped C++ library (Boost.Python), so I can't just hijack __setattr__ in a class and put a condition variable there, which leaves me with either trying to create and signal a Python condition variable from C++, or wrap a native one and wait on it in Python, both of which sound fiddly, needlessly complicated and boring.

Is there an easier way to do it, barring continuous polling of the condition?

Ideally it would be along the lines of

res = wait_until(lambda: some_predicate, timeout)
if (not res):
    print 'timed out'

© Stack Overflow or respective owner

Related posts about python