Production-grade retries made easy.
Actively maintained. Last release 2 weeks ago, commits in 16 of the last 26 weeks. A safe choice.
pip install staminastamina: 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
loggingsupport 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 httpx2
import stamina
@stamina.retry(on=httpx2.HTTPError, attempts=3)
def do_it(code: int) -> httpx2.Response:
resp = httpx2.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:
