Last active
January 29, 2018 08:54
-
-
Save tusharbabbar/b258bb356f46f8218a0009dc33c64134 to your computer and use it in GitHub Desktop.
Configurable retry wrapper in Python
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
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