The `retry` library (version 0.9.2) provides a simple decorator for adding retry logic to Python functions. It allows configuration of exceptions to catch, number of attempts, delay between retries, backoff strategy, and optional jitter. This library is largely unmaintained, with its last release in 2016, and is superseded by more actively developed alternatives like `tenacity`.
pip install retryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to apply the `@retry` decorator to a function, configuring it to retry on specific exceptions, with a maximum number of attempts, an initial delay, exponential backoff, and a maximum delay between retries. It will print logging messages for each retry attempt.
Consider migrating to `tenacity` (pip install tenacity) for active development, better features, and bug fixes. Tenacity offers a more robust and flexible API for retry strategies.
Always set a finite `tries` value (e.g., `tries=5`) or a `max_delay` to prevent infinite loops in production environments.
For asynchronous code, you will need to use a different retry library that explicitly supports `async/await`, such as `tenacity` with `AsyncRetrying` or `stamina`.
Be aware of the type of `jitter` parameter passed. For true random jitter to prevent 'thundering herd' problems, provide a tuple (e.g., `jitter=(0, 1)`).
Run 'pip install retry' in your terminal.
Always use parentheses when providing arguments to the decorator, even if empty: `@retry(tries=3)` instead of `@retry(tries=3)`.
If you `import retry`, use `@retry(...)` or `retry(func, ...)`. If you `from retry import retry`, then use `@retry(...)` or `retry(func, ...)`.
Replace unsupported arguments like 'wait' with the correct argument 'delay' (e.g., `@retry(delay=2)` instead of `@retry(wait=2)`).