Configs2026年9月11日·1 分钟阅读

Tenacity — Robust Retry Library for Python

A general-purpose Python retrying library that simplifies adding retry behavior to any function or code block with configurable backoff strategies.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Tenacity Overview
先审查命令
npx -y tokrepo@latest install 7f193a00-ae2e-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

Tenacity is a Python library that provides a clean decorator-based API for retrying operations that may fail transiently. It is the actively maintained fork of the retrying library, offering more features and better composability for production retry logic.

What Tenacity Does

  • Retries any callable with a simple @retry decorator
  • Supports exponential backoff, fixed delay, and random jitter wait strategies
  • Lets you stop retrying after N attempts, a time limit, or a custom condition
  • Filters which exceptions trigger a retry and which propagate immediately
  • Provides callbacks for logging, metrics, or side effects on each retry

Architecture Overview

Tenacity wraps the decorated function in a retry controller that catches specified exceptions and re-invokes the function according to the configured wait and stop policies. Each retry attempt is tracked in a RetryCallState object that carries attempt count, elapsed time, and the last result or exception. The library is fully synchronous by default but also supports asyncio, Tornado, and trio coroutines.

Self-Hosting & Configuration

  • Install from PyPI with pip install tenacity
  • Apply the @retry decorator to any function or method
  • Combine stop, wait, and retry conditions using the | and & operators
  • Use before_sleep callbacks to log retry attempts
  • Access retry statistics via the function's retry.statistics attribute

Key Features

  • Composable stop, wait, and retry predicates with operator overloading
  • Built-in support for asyncio, Tornado, and trio async frameworks
  • Reraise option to surface the original exception after all retries fail
  • Context manager form for retrying inline code blocks
  • No external dependencies beyond the Python standard library

Comparison with Similar Tools

  • urllib3 Retry — HTTP-specific retry built into urllib3, not general-purpose
  • backoff — similar decorator approach but fewer composability options
  • stamina — newer retry library focused on production defaults and observability
  • retrying — the original library Tenacity forked from, now unmaintained
  • Custom try/except loops — manual retry code that Tenacity replaces with a one-liner

FAQ

Q: Does Tenacity work with async functions? A: Yes. Use the same @retry decorator on async def functions. Tenacity detects coroutines automatically and awaits them correctly.

Q: Can I retry on specific return values instead of exceptions? A: Yes. Use retry_if_result with a predicate function to retry when the return value is unsatisfactory, such as an empty response.

Q: How do I log each retry attempt? A: Pass a before_sleep callback like before_sleep_log(logger, logging.WARNING) to emit a log line before each retry wait.

Q: Is it thread-safe? A: Yes. Each call gets its own RetryCallState, so concurrent threads retrying the same function do not interfere with each other.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产