Created
June 15, 2024 15:21
-
-
Save Menziess/d1b85707e8005a7e0985181bad68ccea to your computer and use it in GitHub Desktop.
This file contains 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 time import sleep | |
from typing import Any | |
def retry(f, *args, _retries: int = 3, _sleep: float = 0.0, **kwargs) -> Any: | |
"""Retry function call until it succeeds.""" | |
if _retries < 0: | |
raise ValueError('_retries cannot be negative.') | |
tries = _retries + 1 | |
for attempt in range(tries): | |
try: | |
return f(*args, **kwargs) | |
except Exception as e: | |
t = type(e).__name__ | |
if attempt == _retries: | |
raise e | |
sleep(_sleep) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment