How to retry just once on exception in python

2020-07-10 08:17发布

问题:

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)