Last active
April 15, 2020 14:11
-
-
Save evanmiller67/4992dba33f4be176ab45758cace47712 to your computer and use it in GitHub Desktop.
sample retries in python
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
def retry(fxn, **kwargs): | |
num_retries = 3 | |
backoff_factor = 0.3 | |
# status_forcelist = (500, 502, 503, 504) | |
for i in range(num_retries): | |
try: | |
return fxn(kwags) | |
except RetryError as err: | |
if i >= 1 and i < num_retries - 1: # and err.code in status_forcelist: | |
sleep(backoff_factor * 2**(i - 1)) | |
class RetryError(Exception): | |
'''Raise when called function fails and should be retried | |
Attributes: | |
code -- The HTTP status code | |
reason -- The HTTP reason text | |
message -- Message to be logged | |
''' | |
def __init__(self, code, reason, message): | |
self.code = code | |
self.reason = reason | |
self.message = message |
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
import req_retry | |
import requests | |
import json | |
def call_url(url): | |
try: | |
response = reqeusts.request('GET', url)) | |
return json.load(response) | |
except Exception as e: | |
raise RetryError(response.status_code, response.reason, f"Call to {url} failed") | |
req_retry.retry(call_url, | |
url='https://tools.learningcontainer.com/sample-json-file.json', | |
headers={'Accept':'application/json'} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment