"""
realize retrying methods by decorator
interpreter: python v3
author: WuYang Ezra
date: 2021-04-26
sample result:
request to baidu, method is get and timeout is 10...
res=4, attempt to retry (1 time)...
request to baidu, method is get and timeout is 10...
res=5, attempt to retry (2 time)...
request to baidu, method is get and timeout is 10...
res=1, attempt to retry (3 time)...
request to baidu, method is get and timeout is 10...
after 3 times. still failed! res = 2
"""
import random
def retries(max_time):
def deco(func):
def inner(url, method, timeout):
res = func(url, method, timeout)
i = 0
while res and i < max_time:
print(f'res={res}, attempt to retry ({i+1} time)...')
res = func(url, method, timeout)
i += 1
return res
return inner
return deco
python装饰器实现方法重试
最新推荐文章于 2024-04-27 17:32:44 发布