-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbf5053
commit 2dc08cf
Showing
2 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import time | ||
from functools import wraps | ||
|
||
|
||
def retry(exception, tries=4, delay=3, backoff=2, logger=None): | ||
"""Retry calling the decorated function using an exponential backoff. | ||
:type exception: Exception or tuple | ||
:param exception: the exception to check. may be a tuple of | ||
exceptions to check | ||
:type tries: int | ||
:param tries: number of times to try (not retry) before giving up | ||
:type delay: int | ||
:param delay: initial delay between retries in seconds | ||
:type backoff: int | ||
:param backoff: backoff multiplier e.g. value of 2 will double the delay | ||
each retry | ||
:type logger: logging.Logger instance | ||
:param logger: logger to use. If None, print | ||
:rtype: func | ||
:returns: Retry wrapper function. | ||
""" | ||
def retry_decorator(f): | ||
|
||
@wraps(f) | ||
def f_retry(*args, **kwargs): | ||
mtries, mdelay = tries, delay | ||
while mtries > 1: | ||
try: | ||
return f(*args, **kwargs) | ||
except exception as e: | ||
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay) | ||
if logger: | ||
logger.warning(msg) | ||
else: | ||
print(msg) | ||
time.sleep(mdelay) | ||
mtries -= 1 | ||
mdelay *= backoff | ||
return f(*args, **kwargs) | ||
|
||
return f_retry | ||
|
||
return retry_decorator |