Skip to content

Instantly share code, notes, and snippets.

@andypern
Last active June 20, 2016 17:47
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 andypern/42a4bd8c0fef661024478b46c842d4a1 to your computer and use it in GitHub Desktop.
Save andypern/42a4bd8c0fef661024478b46c842d4a1 to your computer and use it in GitHub Desktop.
max_retries = 5
try:
do_stuff(objKey)
except (somerror) as e:
logger.error('got a timeout for %s -> %s', objKey, e)
#
#retry until we either succeed, or reach max_retries
#
for attempt in range(max_retries):
#
#exponential backoff
#
print "sleeping for %s secs before retry" %(2 ** attempt)
time.sleep(2 ** attempt)
try:
do_stuff(objKey)
print "shit worked"
break
except (somerror) as e:
if attempt < (max_retries - 1):
logger.error('attempt %s for %s failed with %s, %s remaining', attempt + 1, objKey,e,max_retries - attempt)
continue
else:
logger.error('All %s attempts for %s failed, aborting this file',max_retries,objKey)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment