Skip to content

Instantly share code, notes, and snippets.

@antunesleo
Created September 1, 2023 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antunesleo/7fa5e8a3f7234f9e2aa75874f5a6f97e to your computer and use it in GitHub Desktop.
Save antunesleo/7fa5e8a3f7234f9e2aa75874f5a6f97e to your computer and use it in GitHub Desktop.
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