Wiki

Clone wiki

python-commodity / Home

Python commodity tools

os_.wait_or_raise()

wait_or_raise(func, delta=0.5, timeout=5, exc=AssertionError)

Test a predicate (function without arguments) until it becomes true or the timeout is reached. The predicate is evaluated each 'delta' seconds until a maximum of 'timeout' seconds. If the timeout is reached, the 'exc' exception will be raised.

If you want to uses arbitrary functions (with any number or arguments) as predicate, use functools.partial to convert them.

Next example poll the TCP port 2000 is open each 0.5 secs. If the port remains closed after 2 seconds, it raises AssertionError exception.

wait_or_raise(functools.partial(is_port_open, 2000), delta=0.5, timeout=2)

Updated