Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retry just once on exception in python

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?

like image 560
Slater Victoroff Avatar asked Aug 09 '13 17:08

Slater Victoroff


People also ask

Can you have two excepts in Python?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.


1 Answers

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)
like image 121
Steven Rumbalski Avatar answered Sep 19 '22 16:09

Steven Rumbalski