Created
November 12, 2013 22:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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