Skip to content

Instantly share code, notes, and snippets.

@armonge
Created August 13, 2014 14:30
Show Gist options
  • Save armonge/b1b9b54a7add1c3fbd24 to your computer and use it in GitHub Desktop.
Save armonge/b1b9b54a7add1c3fbd24 to your computer and use it in GitHub Desktop.
Decorator for retrying a function if it fails
def retry(exception):
def retry_decorator(func):
def _wrapper(*args, **kwargs):
tries = 0
while True:
try:
return func(*args, **kwargs)
except exception as error:
tries += 1
if tries <= 5:
seconds = 2**(tries+2)
print(error)
print('Waiting {0} seconds'.format(seconds))
time.sleep(seconds)
else:
raise error
return _wrapper
return retry_decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment