Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def backoff(test_func, reason=None):
- def decorator(func):
- @wraps(func)
- def inner(*args, **kwargs):
- if test_func(*args, **kwargs):
- return None
- result = None
- for wait_secs in [0, 1, 4, 9, 16, 32, 64, 128, 256, None]:
- result = func(*args, **kwargs)
- if test_func(*args, **kwargs):
- break
- if wait_secs is not None:
- log.info("Waiting %s seconds.", wait_secs)
- time.sleep(wait_secs)
- else:
- if reason:
- raise Exception(reason)
- else:
- raise Exception('%s failed.' % func)
- return result
- return inner
- return decorator
- def is_authenticated(xyz):
- try:
- authd = xyz.authenticate() # returns True if successful, otherwise False.
- except Exception:
- return False # Or, it might raise an Exception.
- return authd
- @backoff(is_authenticated, reason="Cannot Authenticate.")
- def do_xyz_things(xyz):
- xyz.get('/things')
- ...
- if __name__ == '__main__':
- do_xyz_things(XyzApiClient(host))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement