retry2
An easy-to-use, dependency-free retry decorator for Python functions.
Repository Health
Technical Analysis
retry2 is a small Python library that provides an easy-to-use retry decorator for automatically re-running functions that raise exceptions. A maintained fork of the original (unmaintained) retry package, it lets you wrap any callable so that transient failures — flaky network calls, temporary lock contention, rate limits — are retried according to a configurable policy instead of bubbling up immediately.
The decorator supports a full set of tuning knobs: which exceptions to catch, the maximum number of attempts, initial delay, maximum delay, exponential backoff multiplier, and fixed or random jitter. It preserves the original traceback for easy debugging, optionally preserves function signatures when the decorator package is installed, and depends only on the standard library at runtime.
What You Get
- A @retry decorator with configurable exceptions, tries, delay, max_delay, backoff, and jitter
- A retry_call helper for retrying a function invocation without decorating it
- Fixed or random jitter and exponential backoff for spacing out attempts
- Original traceback preservation for easy debugging of the final failure
- Zero required runtime dependencies (standard library only)
Common Use Cases
- Retrying flaky network or API calls that occasionally time out or rate-limit
- Re-attempting operations that hit transient database locks or contention
- Adding backoff-and-retry behavior to background jobs without extra infrastructure
Under The Hood
Architecture - The library is tiny and focused. retry/api.py (~120 lines) implements the core __retry_internal loop that catches the configured exceptions, sleeps for a delay that grows by the backoff multiplier and is perturbed by jitter up to max_delay, and re-invokes the target until tries is exhausted. Public entry points retry (the decorator) and retry_call (imperative) wrap that loop, while compat.py provides the optional signature-preserving decorator glue.
Tech Stack - Pure Python packaged with setuptools via setup.py/setup.cfg, tested across versions with tox. The only declared dependency is decorator (used optionally to preserve wrapped function signatures); at runtime the retry logic itself uses just the standard library.
Code Quality - The codebase is small and readable with a dedicated tests/test_retry.py suite and a tox configuration for multi-version testing. It is a mature fork with low recent activity, but its narrow scope means the surface area is stable and easy to audit.
API Design - The API is deliberately minimal and well documented: a single retry(...) decorator with keyword arguments covering exceptions, tries, delay, max_delay, backoff, jitter, logger, and an on_exception hook. Sensible defaults mean @retry() works out of the box, and retry_call offers the same behavior without decorating, keeping the learning curve very low.