Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/retry/api.py: 91%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import logging
2import random
3import time
5from functools import partial
7from .compat import decorator
10logging_logger = logging.getLogger(__name__)
13def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0,
14 logger=logging_logger):
15 """
16 Executes a function and retries it if it failed.
18 :param f: the function to execute.
19 :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
20 :param tries: the maximum number of attempts. default: -1 (infinite).
21 :param delay: initial delay between attempts. default: 0.
22 :param max_delay: the maximum value of delay. default: None (no limit).
23 :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
24 :param jitter: extra seconds added to delay between attempts. default: 0.
25 fixed if a number, random if a range tuple (min, max)
26 :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
27 default: retry.logging_logger. if None, logging is disabled.
28 :returns: the result of the f function.
29 """
30 _tries, _delay = tries, delay
31 while _tries:
32 try:
33 return f()
34 except exceptions as e:
35 _tries -= 1
36 if not _tries:
37 raise
39 if logger is not None:
40 logger.warning('%s, retrying in %s seconds...', e, _delay)
42 time.sleep(_delay)
43 _delay *= backoff
45 if isinstance(jitter, tuple):
46 _delay += random.uniform(*jitter)
47 else:
48 _delay += jitter
50 if max_delay is not None:
51 _delay = min(_delay, max_delay)
54def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
55 """Returns a retry decorator.
57 :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
58 :param tries: the maximum number of attempts. default: -1 (infinite).
59 :param delay: initial delay between attempts. default: 0.
60 :param max_delay: the maximum value of delay. default: None (no limit).
61 :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
62 :param jitter: extra seconds added to delay between attempts. default: 0.
63 fixed if a number, random if a range tuple (min, max)
64 :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
65 default: retry.logging_logger. if None, logging is disabled.
66 :returns: a retry decorator.
67 """
69 @decorator
70 def retry_decorator(f, *fargs, **fkwargs):
71 args = fargs if fargs else list()
72 kwargs = fkwargs if fkwargs else dict()
73 return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
74 logger)
76 return retry_decorator
79def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
80 jitter=0,
81 logger=logging_logger):
82 """
83 Calls a function and re-executes it if it failed.
85 :param f: the function to execute.
86 :param fargs: the positional arguments of the function to execute.
87 :param fkwargs: the named arguments of the function to execute.
88 :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
89 :param tries: the maximum number of attempts. default: -1 (infinite).
90 :param delay: initial delay between attempts. default: 0.
91 :param max_delay: the maximum value of delay. default: None (no limit).
92 :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
93 :param jitter: extra seconds added to delay between attempts. default: 0.
94 fixed if a number, random if a range tuple (min, max)
95 :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
96 default: retry.logging_logger. if None, logging is disabled.
97 :returns: the result of the f function.
98 """
99 args = fargs if fargs else list()
100 kwargs = fkwargs if fkwargs else dict()
101 return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger)