Last active
October 19, 2025 19:43
-
-
Save alairock/a0235eae85c62f0f0f7b81bec8aa378a to your computer and use it in GitHub Desktop.
Python Async Retry Decorator.
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
| class TooManyTriesException(BaseException): | |
| pass | |
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
| 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