Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- import itertools
- import logging
- import time
- log = logging.getLogger(__name__)
- def retry(times, timeout=0.2, exceptions=None):
- """A retry decorator.
- :param times: integer number of times to retry
- :param timeout: floating point number of second to wait between calls
- :param exceptions: tuple of exceptions to retry on. defaults to all
- """
- if exceptions is None:
- exceptions = (Exception,)
- else:
- exceptions = tuple(exceptions)
- def wrapper(fn):
- @functools.wraps(fn)
- def decorated(*args, **kwargs):
- for attempt in itertools.count(1):
- log.debug("Calling function '%s' attempt '%s'",
- fn.__name__, attempt)
- try:
- return fn(*args, **kwargs)
- except exceptions:
- if attempt > times:
- raise
- if timeout:
- time.sleep(timeout)
- return decorated
- return wrapper
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement