【发布时间】:2020-09-28 21:44:49
【问题描述】:
假设我有以下功能:
@retry(stop=stop_after_attempt(3))
def foo():
try:
response = requests.post(...)
response.raise_for_status()
return response
except Exception as e:
raise e
这个函数会重试3次,如果3次都失败,就会抛出异常。
如何在不引发异常的情况下使用坚韧进行 3 次重试?比如:
@retry(stop=stop_after_attempt(3))
def foo(ignore_errors=False):
try:
response = requests.post(...)
response.raise_for_status()
return response
except Exception as e:
if ignore_errors and function has been retried three times:
pass
raise e
【问题讨论】:
-
只需删除
raise,并将其替换为print("Oh my god, there was an error, call the fire department!")。 -
但是如果我不在异常中引发错误,它会如何触发重试呢?
标签: python python-requests python-tenacity