retry#

composer.utils.retry(exc_class=<class 'Exception'>, num_attempts=3, initial_backoff=1.0, max_jitter=0.5)[source]#

Decorator to retry a function with backoff and jitter.

Attempts are spaced out with initial_backoff + 2**num_attempts + random.random() * max_jitter seconds.

Example: .. testcode:

from composer.utils import retry

num_tries = 0

@retry(RuntimeError, num_attempts=3, initial_backoff=0.1)
def flaky_function():
    global num_tries
    if num_tries < 2:
        num_tries += 1
        raise RuntimeError("Called too soon!")
    return "Third time's a charm."

print(flaky_function())
Third time's a charm.
Parameters
  • exc_class (Type[Exception] | Sequence[Type[Exception]]], optional) โ€“ The exception class or classes to retry. Defaults to Exception.

  • num_attempts (int, optional) โ€“ The total number of attempts to make. Defaults to 3.

  • initial_backoff (float, optional) โ€“ The initial backoff, in seconds. Defaults to 1.0.

  • max_jitter (float, optional) โ€“

    The maximum amount of random jitter to add. Defaults to 0.5.

    Increasing the max_jitter can help prevent overloading a resource when multiple processes in parallel are calling the same underlying function.