Skip to content

Instantly share code, notes, and snippets.

@evanmiller67
Last active April 15, 2020 14:11
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 evanmiller67/4992dba33f4be176ab45758cace47712 to your computer and use it in GitHub Desktop.
Save evanmiller67/4992dba33f4be176ab45758cace47712 to your computer and use it in GitHub Desktop.
sample retries in python
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
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