Skip to content

Instantly share code, notes, and snippets.

@Menziess
Created June 15, 2024 15:21
Show Gist options
  • Save Menziess/d1b85707e8005a7e0985181bad68ccea to your computer and use it in GitHub Desktop.
Save Menziess/d1b85707e8005a7e0985181bad68ccea to your computer and use it in GitHub Desktop.
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