Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2013 23:54
Show Gist options
  • Save anonymous/4439504 to your computer and use it in GitHub Desktop.
Save anonymous/4439504 to your computer and use it in GitHub Desktop.
exponential backoff decorator
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