Created
September 1, 2023 16:51
-
-
Save antunesleo/7fa5e8a3f7234f9e2aa75874f5a6f97e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep | |
import requests | |
UNSTABLE_API = "https://httpbin.org/status/200,500" | |
def notsodumbretry(max_attempts, retry_backoff, backoff_exponential): | |
succeeded = False | |
attempts = 0 | |
while attempts <= max_attempts: | |
response = requests.get(UNSTABLE_API) | |
succeeded = response.ok | |
attempts += 1 | |
if succeeded: | |
break | |
sleep(retry_backoff) | |
if backoff_exponential: | |
retry_backoff = retry_backoff * 2 | |
if succeeded: | |
print(f"succeded after {attempts} attempts") | |
else: | |
print(f"failed after {attempts} attemps") | |
if __name__ == "__main__": | |
notsodumbretry(max_attempts=3, retry_backoff=4, backoff_exponential=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment