Skip to content

Instantly share code, notes, and snippets.

@Flushot
Created June 14, 2013 22:55
Show Gist options
  • Select an option

  • Save Flushot/5785928 to your computer and use it in GitHub Desktop.

Select an option

Save Flushot/5785928 to your computer and use it in GitHub Desktop.
Retries a function call on failure
def retry(fn, times=1, delay=3, exc=[Exception]):
"""Calls :fn: and retries up to :times: times with a delay of :delay: while exceptions :exc: are raised"""
import time
for i in range(times):
try:
return fn()
except tuple(exc), ex:
if i < times:
if delay is not None:
time.sleep(delay)
else:
raise ex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment