def retry(num=5, sleep=1):
def decorator(func):
def warpper(*args, **kwargs):
i = 1
while i < num:
try:
result = func(*args, **kwargs)
if 200 == result:
if i == 1:
print('{} The first {} It was a success '.format(func.__name__, i))
return result
except Exception as ex:
file_name = func.__name__
lineno = ex.__traceback__.tb_next.tb_lineno if ex.__traceback__.tb_next else ex.__traceback__.tb_lineno
print('{}_{} That's ok : error '.format(file_name, lineno, i))
finally:
print(func.__name__,i,' Time error ')
i += 1
time.sleep(sleep)
return warpper
return decorator