Skip to content

Instantly share code, notes, and snippets.

@marcinhlybin
Created November 25, 2017 10:08
Show Gist options
  • Save marcinhlybin/a8fefcb596ef4349f09e2cef2cb8e0f0 to your computer and use it in GitHub Desktop.
Save marcinhlybin/a8fefcb596ef4349f09e2cef2cb8e0f0 to your computer and use it in GitHub Desktop.
Retry HTTP connections on 5xx, simple wrapper
#!/usr/bin/env python
import requests
import time
counter = 0
max_retries = 10
def retry(func, *args, **kwargs):
for retry in range(1, max_retries+1):
try:
return func(*args, **kwargs)
except requests.exceptions.HTTPError:
args_params = tuple(args)
kwargs_params = tuple(str(k) + '=' + str(v) for k,v in kwargs.items())
params = ', '.join(args_params + kwargs_params)
print("Error while processing {}({})".format(func.__name__, params))
time.sleep(retry)
continue
else:
raise Exception('Max retries exhausted')
def do_request(url, status):
global counter
counter += 1
url = url + str(status)
print('Doing request to {}'.format(url))
r = requests.get(url)
if r.status_code >= 500 and counter < 3:
r.raise_for_status()
print('Returning {}'.format(r.status_code))
return r.status_code
def do_something():
res = retry(do_request,
url='http://httpbin.org/status/',
status=502
)
return res
if __name__ == '__main__':
print(do_something())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment