Tenacity
Retry library for Python offering decorators and callables with stop, wait, and retry condition strategies.
Official Documentation ↗v8. Check the official docs for the latest API surface.Installation
pip install tenacityMethods
No methods match this filter.
Retry library for Python offering decorators and callables with stop, wait, and retry condition strategies.
Overview
Tenacity retries callables through composable strategies: stop decides when to give up, wait shapes the delay between attempts, and retry picks which outcomes deserve another try. The decorator form covers most code, while Retrying exposes the same policy as a loop or an imperative call.
Hooks such as before_sleep_log and retry_state make attempts observable, and reraise keeps the original exception for callers. The library is dependency-free and works with sync and async functions alike.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
@retry— Decorator retrying the wrapped callable with the configured stopping and waiting strategies.retry_if_exception_type— Restricts retries to the listed exception types, letting business errors fail immediately.before_sleep_log / retry_state— Hook for logging or metrics between attempts; RetryState exposes the attempt number and last outcome.Retrying / retry_call— Applies retry policy imperatively to a single call without decorating the function.
Usage Notes
- Pair a retry condition with the exception types that are genuinely transient; blanket retries amplify outages.
- Cap attempts and total delay together with a stop strategy; unbounded loops hide persistent failures.
- Log every attempt at warning level so incident timelines show real request counts.
Common Pitfalls
- Retrying non-idempotent operations can duplicate side effects such as duplicate orders.
- Without reraise, exhaustion surfaces as RetryError, breaking except clauses for the real error.
- Decorators apply at import time; changing options at runtime requires Retrying instances instead.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://tenacity.readthedocs.io/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:57.986Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- The exception your code encountered will be shown somewhere in the middle of the stack trace. (https://tenacity.readthedocs.io/)
- If you would rather see the exception your code encountered at the end of the stack trace (where it is most visible), you can set reraise=True . (https://tenacity.readthedocs.io/)
- DEBUG )) def raise_my_exception (): raise MyException ( “Fail” ) In the same spirit, It’s possible to execute after a call that failed: import logging import sys logging . (https://tenacity.readthedocs.io/)
FAQ
- What does reraise=True do?
- By default, when retries are exhausted tenacity raises its own RetryError wrapping the last exception. reraise re-raises the original exception so callers see the real failure type and traceback.
- How do I add jitter to waits?
- Use wait_random_exponential or compose wait_random(min, max) with another wait strategy. Jitter prevents synchronized retry storms when many clients fail at the same moment.
- Can retry values depend on the response?
- Yes. retry_if_result(predicate) inspects return values, and wait strategies can be custom callables receiving retry_state, so a Retry-After header can drive the next delay through your own wait function.
Report a problem with this API docOpens a pre-filled issue on GitHub.