【发布时间】:2019-01-24 07:02:38
【问题描述】:
我正在尝试编写一个装饰器来重复错误函数 N 次,其间睡眠时间越来越长。这是我迄今为止的尝试:
def exponential_backoff(seconds=10, attempts=10):
def our_decorator(func):
def function_wrapper(*args, **kwargs):
for s in range(0, seconds*attempts, attempts):
sleep(s)
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return function_wrapper
return our_decorator
@exponential_backoff
def test():
for a in range(100):
if a - random.randint(0,1) == 0:
print('success count: {}'.format(a))
pass
else:
print('error count {}'.format(a))
'a' + 1
test()
我不断收到错误:
TypeError: our_decorator() missing 1 required positional argument: 'func'
【问题讨论】: