Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for condition in given timeout

Tags:

python

wait

I was wondering if there was a sufficient way or a module to wait for a condition(function that returns bool), in given timeout? Example

def wait_for_condition(condition,  timeout, interval) :
    # implementation 
    # return True if the condition met in given timeout, else return False

Thanks in advance!

like image 843
NI6 Avatar asked Mar 16 '16 15:03

NI6


1 Answers

I would simply roll your own, this seems simple enough :

def wait_until(condition, interval=0.1, timeout=1, *args):
  start = time.time()
  while not condition(*args) and time.time() - start < timeout:
    time.sleep(interval)
like image 193
alpha1554 Avatar answered Sep 28 '22 05:09

alpha1554