retry的使用是用于 访问其他方法或者接口失败的时候,需要多次重复回调 查看具体的使用方法,参数如此之多,选择性使用
class Retrying(object):
def __init__(self,
stop=None, wait=None,
stop_max_attempt_number=None,
stop_max_delay=None,
wait_fixed=None,
wait_random_min=None, wait_random_max=None,
wait_incrementing_start=None, wait_incrementing_increment=None,
wait_exponential_multiplier=None, wait_exponential_max=None,
retry_on_exception=None,
retry_on_result=None,
wrap_exception=False,
stop_func=None,
wait_func=None,
wait_jitter_max=None):
self._stop_max_attempt_number = 5 if stop_max_attempt_number is None else stop_max_attempt_number
self._stop_max_delay = 100 if stop_max_delay is None else stop_max_delay
self._wait_fixed = 1000 if wait_fixed is None else wait_fixed
self._wait_random_min = 0 if wait_random_min is None else wait_random_min
self._wait_random_max = 1000 if wait_random_max is None else wait_random_max
self._wait_incrementing_start = 0 if wait_incrementing_start is None else wait_incrementing_start
self._wait_incrementing_increment = 100 if wait_incrementing_increment is None else wait_incrementing_increment
self._wait_exponential_multiplier = 1 if wait_exponential_multiplier is None else wait_exponential_multiplier
self._wait_exponential_max = MAX_WAIT if wait_exponential_max is None else wait_exponential_max
self._wait_jitter_max = 0 if wait_jitter_max is None else wait_jitter_max
个人测试使用: 仅仅测试个别参数: stop_max_attempt_number stop_max_delay
retry参数说明: retry(wait_fixed = 1000) #设置重试间隔时长(ms 1000ms = 1s) retry(wait_random_min = 1000,wait_random_max = 2000,) #随机重试间隔,将在1~2s内 retry(stop_max_attempt_number = 3) #最大重试次数,超过后正常抛出异常 retry(stop_max_delay = 2000) #最大延迟时长,2s内未满足条件则抛出异常 retry(retry_on_exception = 自定义函数) #当发生指定异常时会执行函数 retry(retry_on_result=自定义函数) #每次都会执行函数,当返回返回True就重试,否则异常退出
from retrying import retry
import random
import time
"""
访问其他方法或者接口失败的时候,需要多次重复回调
"""
class TestTrying:
num_count = 0
def __init__(self, attempts, max_stop):
pass
@retry(stop_max_attempt_number=10, stop_max_delay=2000)
def retries(self):
print("retry times and random value is :" + str(self.num_count))
if self.num_count < 10:
self.num_count = random.randint(0, 10)
raise ValueError
else:
print('succeed!')
print()
t = TestTrying(1, 2)
t.retries()
打印结果:
File "/Users/brandonxian/PycharmProjects/pyspark_start/new_trip_pyspark/PythonUsing/retry_using.py", line 22, in retries
raise ValueError
ValueError
retry times and random value is :0
retry times and random value is :0
retry times and random value is :7
retry times and random value is :7
retry times and random value is :1
retry times and random value is :2
retry times and random value is :3
retry times and random value is :8
retry times and random value is :5
retry times and random value is :0
调整最大重试次数: 更改最大重试次数为4: 结果:
retry times and random value is :0
retry times and random value is :4
retry times and random value is :1
retry times and random value is :5
Traceback (most recent call last):
File "/Users/brandonxian/PycharmProjects/pyspark_start/new_trip_pyspark/PythonUsing/retry_using.py", line 29, in <module>
测试stop_max_delay 调整
重试次数加大,时间很短
@retry(stop_max_attempt_number=100000000, stop_max_delay=3000)
条件改为:
if self.num_count < 100:
再次测试, 发现无论如何会在3秒内触发异常
测试结束
|