Python韧性:如果所有重试都失败,我如何重试函数而不引发异常?

Python韧性:如果所有重试都失败,我如何重试函数而不引发异常?,python,python-requests,python-tenacity,Python,Python Requests,Python Tenacity,假设我有以下功能: @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次重试而不引发异常?比如: @retry(stop=

假设我有以下功能:

@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次重试而不引发异常?比如:

@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
使用纯python:

def foo(tries=0,maxTries=3):
    try:
        response = requests.post(...)
        response.raise_for_status()
        return response
    except Exception as e:
        if tries>=maxTries:
            print("Maxtries reached.")
            return
        else:
            foo(tries+1,maxTries)
我不确定递归函数在这里是否有用。

使用纯python:

def foo(tries=0,maxTries=3):
    try:
        response = requests.post(...)
        response.raise_for_status()
        return response
    except Exception as e:
        if tries>=maxTries:
            print("Maxtries reached.")
            return
        else:
            foo(tries+1,maxTries)

我不确定递归函数是否有帮助。

只需删除raise,并用print之类的内容替换它。哦,天哪,发生了错误,请致电消防部门!。但是,如果我没有在异常中引发错误,它将如何触发重试?只需删除raise,并将其替换为print之类的内容哦,天哪,发生了错误,请致电消防部门!。但是如果我不在异常中引发错误,它将如何触发重试?