【Python】重试库-tenacity【原创】

Python的重试库-tenacity

一、介绍

为了避免由于一些网络或等其他不可控因素,而引起的功能性问题。比如在发送请求时,会因为网络不稳定,往往会有请求超时的问题。

这种情况下,我们通常会在代码中加入重试的代码。重试的代码本身不难实现,但如何写得优雅、易用,是我们要考虑的问题。


这里有一个第三方库 - Tenacity ,它实现了几乎我们可以使用到的所有重试场景,比如:

  1. 在什么情况下才进行重试?
  2. 重试几次呢?
  3. 重试多久后结束?
  4. 每次重试的间隔多长呢?
  5. 重试失败后的回调?

在使用它之前 ,先要安装它

$ pip install tenacity

二、用法

基本用法

简单的使用方法是给需要重试的方法加上@retry装饰器,代码抛出异常之后会被装饰器捕获并进行重试,异常抛出时会不断进行重试直到代码执行成功

例子:无条件重试直到执行成功,重试之间无间隔

from tenacity import retry

@retry
def test_retry():
    print("等待重试,重试无间隔执行...")
    raise Exception

test_retry()

可以使用@wait_fixed来指定重试之前的等待时间,也可使用@wait_random进行随机等待

例子:无条件重试直到成功,但在重试之前要等待 2 秒

from tenacity import retry, wait_fixed

@retry(wait=wait_fixed(2))
def test_retry():
    print("等待重试...")
    raise Exception

test_retry()

随机等待1-2秒再重试

from tenacity import retry, wait_random

@retry(wait=wait_random(min=1, max=2))
def test_retry():
    print("等待重试...")
    raise Exception

test_retry()

设置停止基本条件

可以加上终止条件来停止无条件重试,使用@stop_after_attempt设置最大重试次数

例子:只重试7次

扫描二维码关注公众号,回复: 13730159 查看本文章
from tenacity import retry, stop_after_attempt

@retry(stop=stop_after_attempt(7))
def test_retry():
    print("等待重试...")
    raise Exception

test_retry()

可以使用@stop_after_delay来设置重试后的最长时间,超过时间仍未成功则停止

例子:重试10 秒后不再重试

from tenacity import retry, stop_after_delay

@retry(stop=stop_after_delay(10))
def test_retry():
    print("等待重试...")
    raise Exception

test_retry()

或者上面两个条件满足一个就结束重试

from tenacity import retry, stop_after_delay, stop_after_attempt

@retry(stop=(stop_after_delay(10) | stop_after_attempt(7)))
def test_retry():
    print("等待重试...")
    raise Exception

test_retry()

设置何时进行重试

可以通过设置@retry_if_exception_type参数,只有在出现特定错误/异常的情况下,再进行重试

例子:请求超时才进行重试

from requests import exceptions
from tenacity import retry, retry_if_exception_type

@retry(retry=retry_if_exception_type(exceptions.Timeout))
def test_retry():
    print("等待重试...")
    raise exceptions.Timeout

test_retry()

例子:IO错误才进行重试

from requests import exceptions
from tenacity import retry, retry_if_exception_type

@retry(retry=retry_if_exception_type(IOError))
def test_retry():
    print("等待重试...")
    raise IOError

test_retry()

在满足自定义条件时,再进行重试。

例子:当 test_retry 函数返回值为 False 时,再进行重试

from tenacity import retry, stop_after_attempt, retry_if_result

def is_false(value):
    return value is False

@retry(stop=stop_after_attempt(3),
       retry=retry_if_result(is_false))
def test_retry():
    return False

test_retry()

手动进行重试

可以通过手动抛出TryAgain错误,来触发重试,如下:

from tenacity import retry

@retry
def do_something():
   result = something_else()
   if result == 23:
      raise TryAgain

重试后错误重新抛出

当出现异常后,tenacity 会进行重试,若重试后还是失败,默认情况下,往上抛出的异常会变成 RetryError,而不是最开始的报错原因。

因此可以加一个参数(reraise=True),使得当重试失败后,往外抛出的异常还是原来的那个。

from tenacity import retry, stop_after_attempt

@retry(stop=stop_after_attempt(7), reraise=True)
def test_retry():
    print("等待重试...")
    raise Exception

test_retry()

设置回调函数

当最后一次重试失败后,可以执行一个回调函数

from tenacity import *

def return_last_value(retry_state):
    print("执行回调函数")
    return retry_state.outcome.result()  # 表示返回原函数的返回值

def is_false(value):
    return value is False

@retry(stop=stop_after_attempt(3),
       retry_error_callback=return_last_value,
       retry=retry_if_result(is_false))
def test_retry():
    print("等待重试中...")
    return False

print(test_retry())

输出如下:

等待重试中...
等待重试中...
等待重试中...
执行回调函数
False

参考

猜你喜欢

转载自blog.csdn.net/jiandanokok/article/details/117094996