Retry

class Retry(retries=3, exceptions=None, predicate=None, blacklist=None, deadline=None, initial=0.1, maximum=60, jitter=(0, 1), multiplier=2.0)[source]

Retry class to wrap functions as a decorator or inline.

Example

>>> import descarteslabs as dl
>>> retry = dl.common.retry.Retry(
...     maximum=30,
...     retries=5,
...     exceptions=(dl.exceptions.GatewayTimeoutError,)
... )
>>> @retry
... def flaky(x):
...    return x
>>> flaky("test")
'test'
>>> retry(lambda x: x)("test")
'test'

Instantiate a Retry object that can be used to wrap a callable.

Parameters:
  • retries (int, optional) – The number of retries allowed.
  • exceptions (tuple, optional) – A tuple of Exceptions that should always be retried.
  • predicate (function, optional) –

    A callable that takes an exception and returns either a bool or a Tuple[bool, int]. If the bool value is true, the wrapped callable was determined to be retryable. This can be used for cases with a generic exception with variable attributes.

    If the return was a Tuple[bool, int], the int value will be used as the delay. This can be used for cases where an exception should only be retried after some variable amount of time. This is typically used for handling Retry-After headers in which the server is requesting the client wait for a specific amount of time.

  • blacklist (tuple, optional) – A tuple of Exceptions that should never be retried.
  • deadline (float, optional) – The deadline in seconds for retries.
  • initial (float) – The amount of delay for the before the first retry.
  • maximum (float) – The maximum amount of delay between retries.
  • jitter (tuple, optional) – The bounds for a random amount to be added to each delay.
  • multiplier (float, optional) – The multiple by which the delay increases.
exception RetryError(message, exceptions)[source]

Error raised when the number of retries has been exhausted or the deadline has passed.

property exceptions

Get a list of exceptions that occurred.

Returns:The list of exceptions
Return type:list
truncated_delay_generator(initial=None, maximum=None, jitter=None, multiplier=2.0)[source]

A generator for truncated exponential delay.

Parameters:
  • initial (float) – The amount of delay for the first generated value.
  • maximum (float) – The maximum amount of delay.
  • jitter (tuple, optional) – The bounds for a random amount to be added to each delay.
  • multiplier (float, optional) – The multiple by which the delay increases.