Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aretry-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aretry-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for aretry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9811d1588ca5eb201c7f4f806290945070780d53146a890ad47fda02bd8539c6
MD5 eeef577295bebfc2e78a5e25601f7f49
BLAKE2b-256 f531e2935e6bf42c80362bbd85d24b9f12a242bdffab0eaa6246db16b4ae408f

See more details on using hashes here.

Provenance

The following attestation bundles were made for aretry-0.1.0.tar.gz:

Publisher: publish.yml on SpinnakerSix/aretry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for aretry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4b45739db539dc31674406a4389165de9fcb49aa8bb47a86ea1820d27880f9e
MD5 4632a7e4199c3448a1ee700b81cd8005
BLAKE2b-256 96f3a17634605a034d823a0ed77f8f07ffda773b91807c69ec3f36ef62f276dc

See more details on using hashes here.

Provenance

The following attestation bundles were made for aretry-0.1.0-py3-none-any.whl:

Publisher: publish.yml on SpinnakerSix/aretry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page