blob: 1c7e6046a5a445511d02fb15b676d88c9c9845ae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import functools
import time
def retry(*, tries: int = 30, delay: int = 1):
"""Decorator for retries.
Retry a function until code no longer raises an exception or
max tries is reached.
Example:
@retry(tries=5, delay=1)
def try_something_that_may_not_be_ready():
...
"""
def _retry(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
last_error = None
for _ in range(tries):
try:
func(*args, **kwargs)
break
except Exception as e:
last_error = e
time.sleep(delay)
else:
if last_error:
raise last_error
return wrapper
return _retry
|