Python retrying 库:判断最后一次重试
在 retrying 库中,可以使用'stop_max_attempt_number' 参数来指定最大重试次数。当达到最大重试次数时,会抛出'RetryError' 异常。因此,可以在捕获'RetryError' 异常时,判断是否达到了最大重试次数,即可判断当前是否是最后一次重试。
示例代码如下:
from retrying import retry, RetryError
@retry(stop_max_attempt_number=3)
def my_func():
# do something that may fail and needs to be retried
pass
try:
my_func()
except RetryError:
# maximum number of retries reached, this is the last attempt
pass
原文地址: https://www.cveoy.top/t/topic/nDIR 著作权归作者所有。请勿转载和采集!