我是python的新手。我正在尝试创建一个重试装饰器,当应用于函数时,将继续重试,直到满足某些条件(为简单起见,重试10次)。
def retry():
def wrapper(func):
for i in range(0,10):
try:
func()
break
except:
continue
return wrapper
现在,将重试任何异常。如何更改它以使其在特定异常上重试。例如,我想使用它:
@retry(ValueError, AbcError)
def myfunc():
//do something
我希望仅重试myfunc
会引发ValueError
或AbcError
。
答案 0 :(得分:8)
您可以向tuple
块提供except ..
个例外以捕获:
from functools import wraps
def retry(*exceptions, **params):
if not exceptions:
exceptions = (Exception,)
tries = params.get('tries', 10)
def decorator(func):
@wraps(func)
def wrapper(*args, **kw):
for i in range(tries):
try:
return func(*args, **kw)
except exceptions:
pass
return wrapper
return decorator
catch-all *exceptions
参数将始终生成元组。我也添加了tries
个关键字,因此您也可以配置重试次数:
@retry(ValueError, TypeError, tries=20)
def foo():
pass
演示:
>>> @retry(NameError, tries=3)
... def foo():
... print 'Futzing the foo!'
... bar
...
>>> foo()
Futzing the foo!
Futzing the foo!
Futzing the foo!
答案 1 :(得分:2)
from functools import wraps
class retry(object):
def __init__(self, *exceptions):
self.exceptions = exceptions
def __call__(self, f):
@wraps(f) # required to save the original context of the wrapped function
def wrapped(*args, **kwargs):
for i in range(0,10):
try:
f(*args, **kwargs)
except self.exceptions:
continue
return wrapped
用法:
@retry(ValueError, Exception)
def f():
print('In f')
raise ValueError
>>> f()
In f
In f
In f
In f
In f
In f
In f
In f
In f
In f
答案 2 :(得分:0)
您可以查看错误类:
except Exception as e:
for et in error_types: #(or args)
if isinstance(e, et):
continue
raise e #re-raise