#pattern #python

The simplest method is to use the tenacity package:

1@retry(stop=stop_after_attempt(7))
2def stop_after_7_attempts():
3    print("Stopping after 7 attempts")
4    raise Exception
1@retry(stop=stop_after_delay(10))
2def stop_after_10_s():
3    print("Stopping after 10 seconds")
4    raise Exception
1@retry(wait=wait_random(min=1, max=2))
2def wait_random_1_to_2_s():
3    print("Randomly wait 1 to 2 seconds between retries")
4    raise Exception
1@retry(wait=wait_exponential(multiplier=1, min=4, max=10))
2def wait_exponential_1():
3    print("Wait 2^x * 1 second between each retry starting with 4 seconds, then up to 10 seconds, then 10 seconds afterwards")
4    raise Exception