Skip to main content

Production-grade retries made easy.

Project description

stamina: Production-grade Retries Made Easy

Transient failures are common in distributed systems. To make your systems resilient, you need to retry failed operations. But bad retries can make things much worse.

stamina is an opinionated wrapper around the great-but-unopinionated Tenacity package. Our goal is to be as ergonomic as possible, while doing the right thing by default, and minimizing the potential for misuse. It is the result of years of copy-pasting the same configuration over and over again:

  • Retry only on certain exceptions – or even a subset of them by introspecting them first using a backoff hook.
  • Exponential backoff with jitter between retries.
  • Limit the number of retries and total time.
  • Automatic async support – including Trio.
  • Preserve type hints of the decorated callable.
  • Flexible instrumentation with Prometheus, structlog, and standard library's logging support out-of-the-box.
  • Dedicated support for testing that allows to globally deactivate retries, or to limit the number of retries and to remove backoffs.

For example:

import httpx

import stamina


@stamina.retry(on=httpx.HTTPError, attempts=3)
def do_it(code: int) -> httpx.Response:
    resp = httpx.get(f"https://httpbin.org/status/{code}")
    resp.raise_for_status()

    return resp

Async callables work use the same API and it's possible to retry arbitrary blocks, too. Check out our tutorial for more examples!

Or, if you prefer video, here's a brief introduction into retries and stamina: Watch the video

Release Information

Removed

  • Support for Python 3.8 and 3.9.

Added

  • The type hints for our public API are now also verified using Pyrefly and ty. #124

  • stamina.retry() now retries wrapped generator functions and async generator functions.

    Warning: Being able to asend and athrow into wrapped async generators introduced nontrivial complexity in the implementation and is therefore provisional. If supporting these features causes problems, they may be removed again in a future version. #123

  • An on hook can now return a float or a datetime.timedelta to specify a custom backoff that overrides the default backoff. #103 #125

Fixed

  • Prevent unbounded stop condition when both attempts and timeout are non-None falsy values. #109

  • Default wait_exp_base parameter is now an integer to prevent an OverflowError after the 1023th retry. #104

  • Attempt.next_wait now returns the correct value. #115


Full Changelog →

Credits

stamina is written by Hynek Schlawack and distributed under the terms of the MIT license.

The development is kindly supported by my employer Variomedia AG and all my amazing GitHub Sponsors.

This project would not be possible without the years of incredible work that went into Tenacity.

stamina for Enterprise

Available as part of the Tidelift Subscription.

The maintainers of stamina and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open-source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.

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

stamina-25.2.0.tar.gz (565.7 kB view details)

Uploaded Source

Built Distribution

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

stamina-25.2.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file stamina-25.2.0.tar.gz.

File metadata

  • Download URL: stamina-25.2.0.tar.gz
  • Upload date:
  • Size: 565.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stamina-25.2.0.tar.gz
Algorithm Hash digest
SHA256 fdff938789e8a0c4c496e1ee8a08ee3c7c3351239f235b53e60d4f5964d07e19
MD5 28d44c66f5611bb4b5f7681b44028ae5
BLAKE2b-256 58b78064b246b3d684720080ee8ffbf1dde5caabe852eb9cb53655eb97992af2

See more details on using hashes here.

Provenance

The following attestation bundles were made for stamina-25.2.0.tar.gz:

Publisher: pypi-package.yml on hynek/stamina

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

File details

Details for the file stamina-25.2.0-py3-none-any.whl.

File metadata

  • Download URL: stamina-25.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for stamina-25.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f0de7dba735464c256a31e6372c01b8bb51fb6efd649e6773f4ce804462feea
MD5 4460a7498f726ac2a570749c394823fc
BLAKE2b-256 8f81c525760353dff91ae2e4c42c3f3d9bf0bfeecbb6165cc393e86915f1717d

See more details on using hashes here.

Provenance

The following attestation bundles were made for stamina-25.2.0-py3-none-any.whl:

Publisher: pypi-package.yml on hynek/stamina

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