Skip to content

Instantly share code, notes, and snippets.

@birkin
Last active November 19, 2021 17:34
Show Gist options
  • Select an option

  • Save birkin/a6bf968d8990926414eb4586fc65370f to your computer and use it in GitHub Desktop.

Select an option

Save birkin/a6bf968d8990926414eb4586fc65370f to your computer and use it in GitHub Desktop.
retry with backoff experimentation
import pprint, requests
def query_solr():
url = 'http://solr_url'
r = requests.get( url, timeout=5 )
result = r.json()
return result
if __name__ == '__main__':
result = query_solr()
print( f'\nresult, ``{pprint.pformat(result)}``' )
"""
Decorator from: <https://keestalkstech.com/2021/03/python-utility-function-retry-with-exponential-backoff/>
Good decorator intro:
<https://realpython.com/primer-on-python-decorators/>
Another decorator tutorial, covering passing arguments to the decorator:
<https://www.datacamp.com/community/tutorials/decorators-python>
"""
import datetime, pprint, requests
import random, time
## the decorator definition
def retry_with_backoff( retries=2, backoff_in_seconds=1 ):
def rwb(f):
def wrapper(*args, **kwargs):
x = 0
while True:
try:
return f(*args, **kwargs)
except:
if x == retries:
raise
else:
sleep = ( backoff_in_seconds * 2 ** x + random.uniform(0, 1) )
time.sleep( sleep )
x += 1
return wrapper
return rwb
initial_time = datetime.datetime.now()
@retry_with_backoff( retries=5 )
def query_solr():
elapsed = datetime.datetime.now() - initial_time
print( f'\nabout to try after ``{str(elapsed)}`` seconds' )
url = 'http://solr_url'
r = requests.get( url, timeout=5 )
result = r.json()
return result
if __name__ == '__main__':
result = query_solr()
print( f'\nresult, ``{pprint.pformat(result)}``' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment