Created
November 25, 2017 10:08
-
-
Save marcinhlybin/a8fefcb596ef4349f09e2cef2cb8e0f0 to your computer and use it in GitHub Desktop.
Retry HTTP connections on 5xx, simple wrapper
This file contains 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
#!/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