Created
October 29, 2017 14:36
-
-
Save hirokiky/0d818203a64378325ebbaf08cee7bb7d to your computer and use it in GitHub Desktop.
retry.py
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 functools | |
import time | |
def retry(exception, max_retry, sleep, err_handler): | |
""" Catching exception and retrying | |
""" | |
def dec(f): | |
@functools.wraps(f) | |
def _wrapped(*args, **kwargs): | |
for t in range(1, max_retry + 1): | |
try: | |
return f(*args, **kwargs) | |
except exception as e: | |
err_handler(e, t, max_retry) | |
time.sleep(sleep) | |
raise MaxRetriedError | |
return _wrapped | |
return dec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment