0
点赞
收藏
分享

微信扫一扫

python——retry装饰器


1. 手写代码

import functools
import logging
import time


def retry(tries=3, interval=1):
# 装饰器,任务函数调用时,进行重试三次
def decorate(func):
@functools.wraps
def wrapper(*args, **kwargs):
count = 0
while True:
try:
result = func(*args, **kwargs)
except Exception as e:
count += 1
if count > tries:
raise e
else:
logging.Logger.warning(f"Failed to call func, Exception: -> {e}")
time.sleep(interval)
else:
return result
return wrapper
return


举报

相关推荐

0 条评论