Lightweight retry decorator with async support and exponential backoff — zero dependencies
Project description
aretry
Lightweight retry decorator for sync and async Python functions. Zero dependencies.
invl/retry — the most popular Python retry library — gets 8M+ weekly downloads but hasn't
shipped a release since 2018 and has no async support. This is the modern replacement.
Install
pip install aretry
Usage
from aretry import retry
# Works on sync functions
@retry(times=3, on=requests.Timeout, backoff=2.0)
def fetch():
return requests.get(url, timeout=5)
# Works on async functions — same decorator, no changes needed
@retry(times=3, on=aiohttp.ClientError, backoff=2.0, jitter=True)
async def fetch_async():
async with session.get(url) as r:
return await r.json()
# @retry with no parens uses all defaults (3 attempts, 1s delay, 2x backoff)
@retry
def simple():
...
Parameters
| Parameter | Default | Description |
|---|---|---|
times |
3 |
Total attempts (1 = try once, no retry) |
on |
Exception |
Exception type or tuple of types to catch |
delay |
1.0 |
Initial wait in seconds before first retry |
backoff |
2.0 |
Multiply delay by this after each attempt |
jitter |
False |
Add ±20% random noise to avoid thundering herd |
before_retry |
None |
Callback (exc, attempt, delay) called before each retry |
Backoff example
With times=4, delay=1.0, backoff=2.0:
| Attempt | Wait before next |
|---|---|
| 1 (fail) | 1s |
| 2 (fail) | 2s |
| 3 (fail) | 4s |
| 4 (fail) | raises RetryExhausted |
Error handling
from aretry import RetryExhausted
try:
fetch()
except RetryExhausted as e:
print(f"Gave up after {e.attempts} attempts. Last error: {e.last_exc}")
RetryExhausted is a subclass of Exception. The original exception is also accessible
via standard __cause__ chaining.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aretry-0.1.0.tar.gz.
File metadata
- Download URL: aretry-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9811d1588ca5eb201c7f4f806290945070780d53146a890ad47fda02bd8539c6
|
|
| MD5 |
eeef577295bebfc2e78a5e25601f7f49
|
|
| BLAKE2b-256 |
f531e2935e6bf42c80362bbd85d24b9f12a242bdffab0eaa6246db16b4ae408f
|
Provenance
The following attestation bundles were made for aretry-0.1.0.tar.gz:
Publisher:
publish.yml on SpinnakerSix/aretry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aretry-0.1.0.tar.gz -
Subject digest:
9811d1588ca5eb201c7f4f806290945070780d53146a890ad47fda02bd8539c6 - Sigstore transparency entry: 1761973523
- Sigstore integration time:
-
Permalink:
SpinnakerSix/aretry@6d7b690a292e35e4c1b4883b3e8873ebc42e39c8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/SpinnakerSix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6d7b690a292e35e4c1b4883b3e8873ebc42e39c8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aretry-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aretry-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4b45739db539dc31674406a4389165de9fcb49aa8bb47a86ea1820d27880f9e
|
|
| MD5 |
4632a7e4199c3448a1ee700b81cd8005
|
|
| BLAKE2b-256 |
96f3a17634605a034d823a0ed77f8f07ffda773b91807c69ec3f36ef62f276dc
|
Provenance
The following attestation bundles were made for aretry-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on SpinnakerSix/aretry
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aretry-0.1.0-py3-none-any.whl -
Subject digest:
d4b45739db539dc31674406a4389165de9fcb49aa8bb47a86ea1820d27880f9e - Sigstore transparency entry: 1761973586
- Sigstore integration time:
-
Permalink:
SpinnakerSix/aretry@6d7b690a292e35e4c1b4883b3e8873ebc42e39c8 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/SpinnakerSix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6d7b690a292e35e4c1b4883b3e8873ebc42e39c8 -
Trigger Event:
push
-
Statement type: