Created
April 21, 2015 05:30
-
-
Save dalang/31d4bd34ff5c2f0b031a to your computer and use it in GitHub Desktop.
decorator for retrying function in specific times
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 retry(attempt, raise_on_fail=False): | |
def decorator(func): | |
def wrapper(*args, **kw): | |
att = 0 | |
last_except = None | |
while att < attempt: | |
try: | |
return func(*args, **kw) | |
except Exception as e: | |
att += 1 | |
last_except = e | |
else: | |
if raise_on_fail: | |
raise Exception('Hit retry threshold, failed for {0}' % str(last_except)) | |
return None | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment