978 字
5 分钟
python 自动重试 tenacity
2024-10-15

Github: jd/tenacity: Retrying library for Python (github.com)

安装:

Terminal window
pip install tenacity

平时常常因为一些不确定因素(网络波动, 可能的数据异常), 一个函数可能并不会成功, 需要重新尝试

停止重试 stop#

默认抛出异常无限重试

指定重试次数, stop_after_attempt

from tenacity import retry, stop_after_attempt
counts = 0
# stop=stop_after_attempt(3) 重试三次后停止执行
@retry(stop=stop_after_attempt(3))
def retry_stop_after_attempt():
global counts
counts += 1
print(counts)
if counts < 3:
raise Exception('None')
if __name__ == '__main__':
retry_stop_after_attempt()
print(counts)
1
2
3
3

指定重试时间, stop_after_delay

@retry(stop=stop_after_delay(5))
def retry_stop_after_attempt():
time.sleep(2)
print('5秒后停止重试')
raise Exception()

使用 “或” 操作符

@retry(stop=(stop_after_delay(5) | stop_after_attempt(2)))
def retry_stop_after_attempt():
time.sleep(2)
print('5秒后或者2次重试后停止')
raise Exception()

重试前等待 wait#

默认重试之前不等待

@retry(wait=wait_fixed(2))
def wait_2_s():
print("在重试之前, 等待2秒")
raise Exception
@retry(wait=wait_random(min=1, max=2))
def wait_random_1_to_2_s():
print("在重试之前, 随机等待1到2秒")
raise Exception

触发重试条件 retry#

默认抛出所有异常都进行重试

@retry(retry=retry_if_exception_type(IOError))
def might_io_error():
print("只有触发 IOError 异常时触发重试")
raise IOError
@retry(retry=retry_if_not_exception_type(AttributeError))
def might_attr_error():
print("重试除AttributeError之外的所有异常")
raise AttributeError
@retry(stop=stop_after_attempt(3), reraise=True)
def raise_last_exception():
print("抛出最后一次尝试的异常, 默认抛出 tenacity.RetryError 重试异常")
raise IOError("This is an IOError")

更多方法:

retry_if_exception
retry_if_exception_type
retry_if_not_exception_type
retry_unless_exception_type
retry_if_result
retry_if_not_result
retry_if_exception_message
retry_if_not_exception_message
retry_any
retry_all

重试日志以及统计#

from loguru import logger
from tenacity import retry, stop_after_attempt, after_log, before_sleep_log, wait_random
level_no = logger.level('DEBUG').no
@retry(stop=stop_after_attempt(3),
wait=wait_random(1, 3),
# 打印重试之前日志, 包括睡眠时间以及异常原因, demo: Retrying __main__.tenacity_retry in 2.9134301466421793 seconds as it raised OSError: This is an IOError.
before_sleep=before_sleep_log(logger, level_no),
# 打印重试之后日志, 包括第几次重试以及耗时, demo: Finished call to '__main__.tenacity_retry' after 0.000(s), this was the 1st time calling it.
after=after_log(logger, level_no),
)
def tenacity_retry():
print("重试日志以及统计")
raise Exception
if __name__ == '__main__':
try:
# 捕获最后一次重试异常, 否则后续无法执行
tenacity_retry()
except:
pass
# 打印重试统计信息, demo: {'start_time': 4111.406, 'attempt_number': 3, 'idle_for': 2.6459927531818717, 'delay_since_first_attempt': 2.6400000000003274}
print(tenacity_retry.retry.statistics)

自定义异常回调#

from tenacity import retry, stop_after_attempt, wait_fixed, RetryCallState
def custom_retry_callback(retry_state: RetryCallState):
# 这里可以根据重试次数, 重试异常, 重试结果等信息, 自定义重试逻辑
# 重试次数
print(f"重试次数: {retry_state.attempt_number}")
# 重试异常信息
print(f"重试异常: {retry_state.outcome.exception()}")
# 抛出异常时, 调用 retry_state.outcome.result() 会触发异常
# 或者 raise retry_state.outcome.exception() 主动触发异常
@retry(stop=stop_after_attempt(2),
wait=wait_fixed(1),
# 自定义重试回调函数, stop后触发
retry_error_callback=custom_retry_callback,
)
def tenacity_retry():
print("重试2次, 每次间隔1s, 2次后仍然失败, 触发自定义回调函数custom_retry_callback")
raise ValueError("触发异常")
if __name__ == '__main__':
tenacity_retry()

根据函数返回值判断是否重试

def custom_retry_callback(retry_state: RetryCallState):
# 这里可以根据重试次数, 重试异常, 重试结果等信息, 自定义重试逻辑
# 重试次数
print(f"重试次数: {retry_state.attempt_number}")
# 函数返回结果,
print(f"重试结果: {retry_state.outcome.result()}")
# 重试函数 tenacity_retry
print(retry_state.fn)
# 重试函数参数
print(retry_state.args)
def check_value_is_legal(result):
# 根据函数返回值判断是否重试
return result > 5
@retry(stop=stop_after_attempt(2),
wait=wait_fixed(1),
# 校验函数返回值是否符合要求进行重试
retry=retry_if_result(check_value_is_legal),
# 自定义重试回调函数, stop后触发
retry_error_callback=custom_retry_callback,
)
def tenacity_retry(a, b, c):
print("重试2次, 每次间隔1s, 2次后仍然失败, 触发自定义回调函数custom_retry_callback")
return a + b + c
if __name__ == '__main__':
tenacity_retry(1, 2, 3)

重试代码块#

def tenacity_retry():
for attempt in Retrying(stop=stop_after_attempt(3)):
with attempt:
print("这里会尝试运行3次")
raise Exception('My code is failing!')

异步协程#

异步代码块

import asyncio
from tenacity import AsyncRetrying, stop_after_attempt
async def tenacity_retry():
async for attempt in AsyncRetrying(stop=stop_after_attempt(3)):
with attempt:
asyncio.sleep(1)
print("这里会尝试运行3次")
raise Exception('My code is failing!')
if __name__ == '__main__':
asyncio.run(tenacity_retry())

注解也支持在异步函数上使用

@retry(stop=stop_after_attempt(3))
async def tenacity_retry():
asyncio.sleep(1)
print('retry')
raise Exception('error')

不过在生成器中并不会生效重试, 只会执行一次

from tenacity import stop_after_attempt, retry, TryAgain
@retry(stop=stop_after_attempt(3))
def tenacity_retry():
for i in range(3):
print(i)
if i > 0:
raise TryAgain # 引发TryAain异常显式重试
yield i
if __name__ == '__main__':
for index in tenacity_retry():
pass

更多详细内容看官方文档

Docs: Tenacity — Tenacity documentation

python 自动重试 tenacity
https://blog.tiamix.cn/posts/python-自动重试-tenacity/
作者
Tiam
发布于
2024-10-15
许可协议
CC BY-NC-SA 4.0