Skip to content

Instantly share code, notes, and snippets.

@hirokiky
Created October 29, 2017 14:36
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 hirokiky/0d818203a64378325ebbaf08cee7bb7d to your computer and use it in GitHub Desktop.
Save hirokiky/0d818203a64378325ebbaf08cee7bb7d to your computer and use it in GitHub Desktop.
retry.py
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