Skip to content

Instantly share code, notes, and snippets.

@tusharbabbar
Last active January 29, 2018 08:54
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 tusharbabbar/b258bb356f46f8218a0009dc33c64134 to your computer and use it in GitHub Desktop.
Save tusharbabbar/b258bb356f46f8218a0009dc33c64134 to your computer and use it in GitHub Desktop.
Configurable retry wrapper in Python
import traceback
from functools import wraps
def retry(retries=5, quantum_backoff=5, exception=Exception):
def real_retry(func):
@wraps(func)
def wrapper(*args, **kwargs):
tries = 0
exc = None
while tries <= retries:
try:
return func(*args, **kwargs)
except exception as e:
exc = e
traceback.format_exc(e)
tries += 1
print "sleeping for {}".format(quantum_backoff * tries)
time.sleep(quantum_backoff * tries)
raise exc
return wrapper
return real_retry
# Usage
# @retry()
# def func():
# pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment