Created
June 14, 2013 22:55
-
-
Save Flushot/5785928 to your computer and use it in GitHub Desktop.
Retries a function call on failure
This file contains hidden or 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(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