Skip to content

Instantly share code, notes, and snippets.

@justnoise
Created November 12, 2013 22:19
Show Gist options
  • Select an option

  • Save justnoise/7439826 to your computer and use it in GitHub Desktop.

Select an option

Save justnoise/7439826 to your computer and use it in GitHub Desktop.
decorator that calls f until f returns true or takes longer than timeout
def wait_for_true(timeout=60, sleep=1.0):
def _wait_for_true(f):
def __wait_for_true(*args, **kwargs):
start_time = time.time()
ret_val = False
while (not ret_val and
timeout is not None and
time.time() - start_time < timeout):
ret_val = f(*args, **kwargs)
time.sleep(sleep)
if not ret_val:
raise Exception("Timed out after {} seconds".format(str(timeout)))
return True
return __wait_for_true
return _wait_for_true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment