How to retry after exception – Python Tips

How to retry after exception – Python Tips

Posted on

Do a while True inside your for loop, put your try code inside, and break from that while loop only when your code succeeds.

for i in range(0,100):
    while True:
            # do stuff
        except SomeSpecificException:

Except use except SomeSpecificError: instead of catching all exceptions.

Another example is using the retrying package to retry a failure of a block of a code. Example:

@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
    print("Randomly wait 1 to 2 seconds between retries")



Leave a Reply

Your email address will not be published. Required fields are marked *