python自定义重试装饰器
2021-05-13 18:30
标签:bre fixed wrap app sleep except cep time exception python自定义重试装饰器 标签:bre fixed wrap app sleep except cep time exception 原文地址:https://www.cnblogs.com/wangbin2188/p/13129748.htmlimport functools
import time
# 最大重试次数/重试间隔
def retry(stop_max_attempt_number=10, wait_fixed=2):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
retry_num = 0
while retry_num stop_max_attempt_number:
rs = None
try:
rs = func(*args, **kw)
break
except Exception:
retry_num += 1
time.sleep(wait_fixed)
finally:
if rs:
return rs
return wrapper
return decorator