Skip to content

Instantly share code, notes, and snippets.

@dalang
Created April 21, 2015 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalang/31d4bd34ff5c2f0b031a to your computer and use it in GitHub Desktop.
Save dalang/31d4bd34ff5c2f0b031a to your computer and use it in GitHub Desktop.
decorator for retrying function in specific times
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