实际运行的效果是这样:
retry... times
retry... times
retry... times
Traceback (most recent call last):
File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 407, in __call__
result = fn(*args, **kwargs)
File "demo_retry.py", line 20, in retry_times
raise Exception
Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "demo_retry.py", line 23, in <module>
retry_times()
File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 324, in wrapped_f
return self(f, *args, **kw)
File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 404, in __call__
do = self.iter(retry_state=retry_state)
File "D:\Python39\lib\site-packages\tenacity\__init__.py", line 361, in iter
raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x16628b45460 state=finished raised Exception>]
别担心,你可能在最后一次的时候就不抛出异常了。 3、过一定时间后停止重试
from tenacity import retry, stop_after_delay
import arrow
from time import sleep
@retry(stop=stop_after_delay(10))
def retry_times():
print(arrow.now().format('YYYY-MM-DD HH:mm:ss'))
sleep(1)
raise Exception
retry_times()