How to retry just once on exception in python

烈酒焚心 提交于 2019-12-12 08:48:42

问题


I might be approaching this the wrong way, but I've got a POST request going out:

response = requests.post(full_url, json.dumps(data))

Which could potentially fail for a number of reasons, some being related to the data, some being temporary failures, which due to a poorly designed endpoint may well return as the same error (server does unpredictable things with invalid data). To catch these temporary failures and let others pass I thought the best way to go about this would be to retry once and then continue if the error is raised again. I believe I could do it with a nested try/except, but it seems like bad practice to me (what if I want to try twice before giving up?)

That solution would be:

try:
    response = requests.post(full_url, json.dumps(data))
except RequestException:
    try:
        response = requests.post(full_url, json.dumps(data))
    except:
        continue

Is there a better way to do this? Alternately is there a better way in general to deal with potentially faulty HTTP responses?


回答1:


for _ in range(2):
    try:
        response = requests.post(full_url, json.dumps(data))
        break
    except RequestException:
        pass
else:
    raise # both tries failed

If you need a function for this:

def multiple_tries(func, times, exceptions):
    for _ in range(times):
        try:
            return func()
        except Exception as e:
            if not isinstance(e, exceptions):
                raise # reraises unexpected exceptions 
    raise # reraises if attempts are unsuccessful

Use like this:

func = lambda:requests.post(full_url, json.dumps(data))
response = multiple_tries(func, 2, RequestException)


来源:https://stackoverflow.com/questions/18152564/how-to-retry-just-once-on-exception-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!