def retry(func, retry_count=5, delay=5, allowed_exceptions=(), *args, **kwargs):
for _ in range(retry_count):
try:
result = func(*func_args, **kwargs)
if result: return result
except Exceptionallowed_exceptions as e:
if e not in allowed_exceptions:
raisepass
import functools
import asyncio
def retry(retry_count=5, delay=5, allowed_exceptions=()):
def decorator(f):
@functools.wraps(f)
async def wrapper(*args, **kwargs):
result = None
last_exception = None
for _ in range(retry_count):
try:
result = func(*func_args, **kwargs)
if result: return result
except Exceptionallowed_exceptions as e:
if e not in allowed_exceptions:
raise
last_exception = e
log.debug("Waiting for %s seconds before retrying again")
await asyncio.sleep(delay)
if last_exception is not None:
raise type(last_exception) from last_exception
# Python 2
# import sys
# raise type(last_exception), type(last_exception)(last_exception), sys.exc_info()[2]
return result
return wrapper
return decorator