tranq 是一个 Python 库,通过装饰器提供零模板的错误处理,支持重试、断路器、指标和可插拔的报告器。

Stars

10

7 天增长

暂无数据

Fork 数

2

开放 Issue

0

开源协议

MIT

最近更新

2026-07-27

AI 仓库情报摘要
FR-AI / ANALYSIS

为什么值得关注

它将多种错误处理模式(多种退避策略的重试、断路器、条件重试、有状态重试)以及现代特性(同步/异步、指标、性能分析、模拟错误注入)整合到一致的 API 中,大幅减少样板代码。

适合谁使用

  • 构建弹性 API 或微服务的 Python 开发者
  • 希望用声明式装饰器替代重复 try/except 块的工程师
  • 需要对外部服务调用使用断路器模式的团队
  • 需要模拟错误注入来测试错误处理逻辑的测试人员和开发者

典型使用场景

  • 使用指数退避和抖动重试 API 客户端的临时网络故障
  • 使用断路器防止服务级联故障
  • 批处理场景中重试状态需在多次调用间保持
  • 从单个装饰器将错误记录并报告到多个目标(文件、Sentry、Slack)

项目优势

  • 整洁的装饰器和上下文管理器 API,保持业务逻辑清晰
  • 丰富的重试策略:指数、线性、斐波那契退避、最大延迟、抖动,以及基于异常或结果的条件重试
  • 内置同步和异步断路器,支持可配置的阈值和半开请求
  • 集成指标、性能分析和可插拔报告器,便于可观测性和测试

使用前须知

  • 需要 Python 3.9 或更高版本,不支持旧版本
  • 相对较新的库,社区和生态系统较小,不如 tenacity 或 backoff 成熟
  • 某些高级特性(如有状态重试、自定义退避回调)可能需要更深入理解配置

README 快速开始

🌿 tranq

Calm error handling for Python – decorator-based, zero boilerplate.


🧘 Why tranq?

Writing repetitive try / except blocks clutters your code and hides the business logic.
tranq gives you declarative error handling with decorators, context managers, and a rich set of retry strategies – so you can focus on what your code does, not how it recovers from failures.

FeatureDescription
🧘 TranquilClean, readable, and maintainable.
🔁 Smart retriesExponential, linear, Fibonacci backoff, jitter, and max delay.
🚦 Circuit BreakerPrevent cascading failures (sync & async).
🧪 Conditional retryOn specific exceptions or result values.
📦 Retry groupsAll-or-nothing execution for multiple functions.
📊 Built‑in metrics & profilingMonitor performance and error rates.
📝 Pluggable reportersSend errors to files, Sentry, Slack, or custom destinations.
🧩 Context manager APIUse tranq.retry(...) when decorators aren't ideal.
🔧 Stateful retryPersist attempt count across calls.
🎭 Mock error injectionTest your error handling with ease.

📦 Installation

pip install tranq

Requires Python 3.9 or later.


⚡ Quick Start

Decorator (@handle)

import tranq

@tranq.handle(on=ValueError, retry=3, delay=0.5, backoff=2.0)
def risky():
    # This will be retried up to 3 times with exponential backoff
    ...

Async (@handle_async)

@tranq.handle_async(on=ConnectionError, retry=2, fallback=lambda: "offline")
async def fetch_data():
    ...

Circuit Breaker

cb = tranq.CircuitBreaker(failure_threshold=5, timeout=60)

@tranq.handle(circuit_breaker=cb)
def call_unstable_service():
    ...

Context Manager

with tranq.retry(on=ValueError, retry=2) as ctx:
    result = ctx.run(my_function, arg1, arg2)

Retry Group (all‑or‑nothing)

group = tranq.retry_group(step1, step2, step3, on=Exception, retry=1)
results = group.run()  # if any step fails, all are retried together

🔍 Features in Depth

1. Retry with Backoff

Choose from exponential, linear, or Fibonacci backoff. Add jitter to avoid thundering herds.

@tranq.handle(
    on=TimeoutError,
    ret

项目描述

Calm error handling for Python

相关仓库与替代方案

根据分类、Topic 和编程语言匹配的相似项目。

vercel-labs
精选
vercel-labs GitHub avatar

scriptc

scriptc compiles ordinary TypeScript into small, fast native executables without needing Node.js, V8, or any JavaScript runtime in the binary.

开发者工具代码质量与构建
1,985
programmersd21
精选
programmersd21 GitHub avatar

flow

flow is a minimalist terminal dashboard that displays real-time network throughput with smooth animations, multiple responsive modes, and zero configuration.

开发者工具CLI 与终端
277
jamesob
精选
jamesob GitHub avatar

local-llm

A comprehensive guide for building and configuring a high-end local machine to run state-of-the-art LLMs, with detailed hardware choices, BIOS tuning, and Docker-based model serving.

AI 与机器学习大语言模型
1,660