daily pastebin goal
12%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import functools
  2. def retriable(func):
  3.     @functools.wraps(func)
  4.     def wrapper(*args, **kwargs):
  5.         timeout = kwargs['timeout']
  6.         criteria_satisfied = func(*args, **kwargs)
  7.         while not criteria_satisfied and timeout > 0:
  8.             time.sleep(5)
  9.             timeout -= 5
  10.             criteria_satisfied = func(*args, **kwargs)
  11.         return criteria_satisfied
  12.     return wrapper
  13.  
  14. @retriable
  15. def decorated(ip, timeout=60):
  16.     ... some logic
  17.     return True
  18.  
  19. decorated(ip)
  20.    
  21. import functools
  22. def retriable_with_arg(timeout):
  23.     def retriable(func):
  24.         @functools.wraps(func)
  25.         def wrapper(*args, **kwargs):
  26.             alive_timeout = timeout
  27.             criteria_satisfied = func(*args, **kwargs)
  28.             while not criteria_satisfied and alive_timeout > 0:
  29.                 time.sleep(5)
  30.                 alive_timeout -= 5
  31.                 criteria_satisfied = func(*args, **kwargs)
  32.             return criteria_satisfied
  33.      return wrapper
  34. return retriable
  35.  
  36. @retriable(timeout=60)
  37. def decorated(ip, timeout=60):
  38.     ... some logic
  39.     return True
  40.  
  41. decorated(ip)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top