对python异常函数进行重试数次,python函数重试,<对python异常函数


<对python异常函数进行重试数次>

1.[代码][Python]代码

def retry(times=1,exceptions=None):    exceptions = exceptions if exceptions is not None else Exception    def wrapper(func):        def wrapper(*args,**kwargs):            last_exception =None            for _ in range(times):                try:                    return func(*args, **kwargs)                except exceptions as e:                    last_exception = e            raise last_exception        return wrapper    return wrapperif __name__=="__main__":    @retry(5)    def test():        print("do something")        raise Exception    test()

编橙之家文章,

评论关闭