html tool

2022年10月9日星期日

转:retry

 参考:http://www.codebaoku.com/it-python/it-python-221511.html

retrying

retrying是Python的一个第三方库,它提供一个装饰器函数retry,被装饰的业务函数就会在运行失败的条件下重新执行,默认只要报错就会一直重试,直至执行成功。

可以使用pip install retrying进行安装。

例如下面一段代码,我们使用生成随机数的大小的方式模拟业务的成功与失败,只要是生成的随机数大于2,都视为失败,就会重试,直到生成的随机数小于2:

import random
from retrying import retry


@retry
def random_with_retry():
  if random.randint(0, 10) > 2:
      print("大于2,重试...")
      raise Exception("大于2")
  print("小于2,成功!")
random_with_retry()

运行结果如下:

retry还可以接受一些参数,下面是源码中Retrying类的初始化函数中可选的参数:

  • stop_max_attempt_number:最大重试次数,超过该次数就停止重试
  • stop_max_delay:最大延迟时间(执行这个方法重试的总时间),超过该时间就停止
  • wait_fixed:两次retrying之间的等待时间
  • wait_random_min和wait_random_max:用随机的方式产生两次retrying之间的等待时间
  • wait_incrementing_start和wait_incrementing_increment:每调用一次增加固定时长
  • wait_exponential_multiplier和wait_exponential_max:以指数的形式产生两次retrying之间的等待时间,产生的值为2^previous_attempt_number * wait_exponential_multiplier,previous_attempt_number是前面已经retry的次数,如果产生的这个值超过了wait_exponential_max的大小,那么之后两个retrying之间的停留值都为wait_exponential_max。

特别需要注意的是retry_on_exception参数,它接收一个函数,用法如下:

# 判断异常
def is_MyError(exception):
  print("判断异常", exception)
  print(isinstance(exception, (ValueError, IOError, ConnectionError)))
  return isinstance(exception, (ValueError, IOError, ConnectionError))
@retry(retry_on_exception=is_MyError)
def random_with_retry():
  """
  随机一个0-10之前的整数,大于2抛异常,小于2成功
  :return:
  """
  if random.randint(0, 10) > 2:
      print("大于2,重试...")
      raise ValueError("大于2")
  print("小于2,成功!")
random_with_retry()

这里retry_on_exception参数的大体思想是:接收一个自定义函数is_MyError,在is_MyError函数里判断了是不是属于ValueError, IOError, ConnectionError这三种异常;random_with_retry()函数如果抛出了异常,会去函数is_MyError()判断返回的是True还是False,如果是True则继续重试,如果是False则立即停止并抛出异常。

还有retry_on_result参数,也是接收一个函数,判断业务函数返回哪些结果时需要重试,思想和retry_on_exception参数类似。
我们可以根据自己的需要进行合理的搭配这些参数,达到我们想要的效果。

没有评论:

发表评论