Skip to content

Instantly share code, notes, and snippets.

@alairock
Last active May 13, 2024 18:07
Show Gist options
  • Save alairock/a0235eae85c62f0f0f7b81bec8aa378a to your computer and use it in GitHub Desktop.
Save alairock/a0235eae85c62f0f0f7b81bec8aa378a to your computer and use it in GitHub Desktop.
Python Async Retry Decorator.
class TooManyTriesException(BaseException):
pass
from .exceptions import TooManyTriesException
def tries(times):
def func_wrapper(f):
async def wrapper(*args, **kwargs):
for time in range(times):
print('times:', time + 1)
# noinspection PyBroadException
try:
return await f(*args, **kwargs)
except Exception as exc:
pass
raise TooManyTriesException() from exc
return wrapper
return func_wrapper
@tries(times=3)
async def do_something(my_var):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment