Created
January 2, 2013 23:54
-
-
Save anonymous/4439504 to your computer and use it in GitHub Desktop.
exponential backoff decorator
This file contains 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 exponential_backoff(err, tries=5): | |
"""Decorator @exponential_backoff("Error if exponential backoff | |
failed after # tries") | |
""" | |
def decorator(func): | |
def inner(*args, **kwargs): | |
for n in range(0, tries): | |
try: | |
return func(*args, **kwargs) | |
except: | |
time.sleep((2 ** n) + (random.randint(0, 1000) / 1000.0)) | |
raise Exception(err(n)) | |
return inner | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment