Registry / testing / retry2

retry2

JSON →
library0.9.5pypypi✓ verified 25d ago

retry2 is a Python library that provides an easy-to-use decorator for retrying functions. It is a fork of the unmaintained `invl/retry` library, offering similar functionality with a focus on being pure standard library Python, though it can optionally preserve function signatures with an additional `decorator` dependency. The current version is 0.9.5.

pip install retry2
INSTALL
IMPORT
SIG · RETRY2
R
retry2
testingpythonv0.9.5
Install
1.6s avg
Import
54ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.9.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.055s · 17.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.052s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

retry
from retry import retry
retry_call
from retry import retry_call
For dynamically adjusting retry arguments, `retry_call` takes a function and its arguments.

This example demonstrates using the `@retry` decorator to automatically retry a function that raises an `IOError`. It will attempt 3 times, with an initial 1-second delay that doubles (backoff=2) between attempts.

from retry import retry import time call_count = 0 @retry(exceptions=IOError, tries=3, delay=1, backoff=2) def might_fail_api_call(): global call_count call_count += 1 print(f"Attempt {call_count}: Calling API...") if call_count < 3: raise IOError("Temporary network issue") print("API call successful!") return "Data" try: result = might_fail_api_call() print(f"Final result: {result}") except IOError as e: print(f"Failed after multiple retries: {e}")
Debug
Known issues
gotchaBy default, the `@retry` decorator sets `tries=-1`, leading to infinite retries if no `tries` or `max_delay` is specified. This can cause indefinite blocking or resource exhaustion in production systems.
fix
Always explicitly set a finite `tries` parameter (e.g., `tries=3`) or `max_delay` to limit retry attempts, especially for operations that are not guaranteed to succeed.
affects: 0.1.0 - 0.9.5
gotchaThe default `delay=0` and `backoff=1` can lead to 'retry storms' where failed operations are immediately retried without a pause. This can overwhelm an already struggling service or cause cascading failures.
fix
Use `delay` with a positive value and `backoff` (e.g., `backoff=2` for exponential backoff) to introduce increasing delays between retries, giving the downstream service time to recover. Consider adding `jitter` for random variation.
affects: 0.1.0 - 0.9.5
gotchaThe library explicitly states that preserving function signatures (e.g., for introspection or other decorators) is optional and requires installing the `decorator` package. Without it, `inspect.signature` or other tools might report incorrect signatures.
fix
If preserving function signatures is critical for your application or tooling, ensure `pip install decorator` is included in your project's dependencies alongside `retry2`.
affects: 0.1.0 - 0.9.5
gotchaRetries should only be applied to transient failures (e.g., network hiccups, temporary service unavailability). Retrying on permanent failures (e.g., 4xx client errors, validation errors) will not resolve the issue and will waste resources.
fix
Specify the `exceptions` parameter to catch only known transient exceptions (e.g., `(requests.exceptions.ConnectionError, requests.exceptions.Timeout)`). Avoid retrying on generic `Exception` unless absolutely sure.
affects: All versions
gotchaWhen retrying operations that modify state (e.g., payments, creating resources), ensure the underlying operation is idempotent. Without idempotency, retries can lead to unintended side effects like duplicate charges or entries.
fix
Design the retried function or the downstream service to handle duplicate requests safely, often by using unique request IDs or transaction identifiers.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'retry2'
The `retry2` package is not installed in the active Python environment.
fix
Run `pip install retry2` in your terminal to install the package.
TypeError: exceptions argument must be a single exception type, a tuple of exception types, or None
The `exceptions` argument of the `@retry` decorator received a list of exception types instead of the required tuple or single exception type.
fix
Wrap multiple exception types in a tuple, e.g., `@retry(exceptions=(ValueError, TypeError))`.
NameError: name 'retry' is not defined
The `@retry` decorator was used without explicitly importing `retry` from the `retry2` package, or without referencing it as `retry2.retry`.
fix
Change your import statement to `from retry2 import retry` or use `@retry2.retry`.
RecursionError: maximum recursion depth exceeded
This error can occur in specific scenarios when `retry2` is used with a `timeout` argument, potentially due to an interaction between the retry loop and the timeout mechanism, especially in older versions of the library.
fix
Update `retry2` to the latest version using `pip install --upgrade retry2`. If the issue persists, review the decorated function for unintended recursion or consider handling timeout logic externally.
Upgrade
Version history
0.9.5latest on PyPI · released Jan 11, 2023
Audit
Dependencies
decoratoroptionalPreserves function signatures when using the decorator; optional.
Agent activity
10 hits · last 30 days
node
8
Resources