Skip to content

Decorators

retry(exceptions, tries=4, delay=3, backoff=2, fatal_exceptions=None, logger=None)

Retry calling the decorated function using an exponential backoff.

Parameters:

Name Type Description Default
exceptions Union[Type[Exception], Tuple[Type[Exception]]]

The exception to check. may be a tuple of exceptions to check.

required
tries int

Number of times to try (not retry) before giving up.

4
delay int

Initial delay between retries in seconds.

3
backoff int

Backoff multiplier (e.g. value of 2 will double the delay each retry).

2
logger

Logger to use. If None, print.

None
Source code in pygsuite/utility/decorators.py
def retry(  # noqa: C901
    exceptions: Union[Type[Exception], Tuple[Type[Exception]]],
    tries: int = 4,
    delay: int = 3,
    backoff: int = 2,
    fatal_exceptions: Optional[Tuple] = None,
    logger=None,
):
    """
    Retry calling the decorated function using an exponential backoff.

    Args:
        exceptions: The exception to check. may be a tuple of
            exceptions to check.
        tries: Number of times to try (not retry) before giving up.
        delay: Initial delay between retries in seconds.
        backoff: Backoff multiplier (e.g. value of 2 will double the delay
            each retry).
        logger: Logger to use. If None, print.
    """
    fatal_exceptions = fatal_exceptions or ()

    def deco_retry(f):
        @wraps(f)
        def f_retry(*args, **kwargs):
            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return f(*args, **kwargs)
                except fatal_exceptions as e:
                    if logger:
                        logger.error(f"Fatal exception, not retrying {str(e)}")
                    raise e
                except exceptions as e:
                    msg = "{}, Retrying in {} seconds...".format(e, mdelay)
                    if logger:
                        logger.warning(msg)
                    else:
                        print(msg)
                    # add some additional jitter between 0 and 5 seconds
                    mdelay += randint(0, 50) / 10
                    time.sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return f(*args, **kwargs)

        return f_retry

    return deco_retry