Created
August 13, 2014 14:30
-
-
Save armonge/b1b9b54a7add1c3fbd24 to your computer and use it in GitHub Desktop.
Decorator for retrying a function if it fails
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(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