Here's a generalized decorator that lets you specify the number of retries and also the exception type to ignore:
from functools import wraps
def retry(count=5, exc_type=Exception):
def decorator(func):
@wraps(func):
def result(*args, **kwargs):
for _ in range(count):
try:
return func(*args, **kwargs)
except exc_type:
pass
raise
return result
return decorator
Use it like this:
@retry(count=10, exc_type=IOError)
def read_file():
pass # do something