python 为函数加上自动retry 的装饰器
"""
:param delays: An iterable object the defines num of seconds between reties, also indicating num of retries.
Default: retry 3 times, waiting 1/5/30 seconds.
:param exceptions: Specifies exceptions that trigger retry. Otherwise no actions will be taken. Default: all exceptions.
:param report: An callable object, e.g print or logger method. Default: report nothing.
"""
def retry(delays=(3, 10, 15), exceptions=(Exception, ), report=lambda *args: None):
def wrapper(function):
@functools.wraps(function)
def wrapped(*args, **kwargs):
problems = []
for delay in itertools.chain(delays, [None]):
try:
return function(*args, **kwargs)
except exceptions as problem:
problems.append(problem)
if delay is None:
report("Function {func} failed after {num} retries. Exceptions: {p}".format(func=function.__name__, num=len(delays), p=problems))
raise
else:
report("Function {func} fails of {expt_name}({expt_msg}). Will retry in {delay} second(s)."\
.format(func=function.__name__, expt_name=type(problem).__name__, expt_msg=problem, delay=delay))
time.sleep(delay)
return wrapped
return wrapper
上面这个装饰器函数来自某位大神之手,先膜拜一下,它能自动为被装饰的函数加上retry功能。
根据上面的代码,会retry三次,每次间隔3,10,15秒。
@functools.wraps(function)的作用是使被装饰的函数保留原有的函数名字和函数doc。
这个函数设计的很巧妙,比如 report=lambda *args: None
作者定义了"返回为None的函数"作为“report”参数的默认值。