Retry decorator Parameters ---------- attempts : int, default 3 The number of attempts. delay : float, default 1 Initial delay in seconds. max_delay : float, optional The max delay between attempts. backoff : float, default 1 The multipli
(attempts=3, delay=1.0, max_delay=None, backoff=1)
| 162 | |
| 163 | |
| 164 | def retry(attempts=3, delay=1.0, max_delay=None, backoff=1): |
| 165 | """ |
| 166 | Retry decorator |
| 167 | |
| 168 | Parameters |
| 169 | ---------- |
| 170 | attempts : int, default 3 |
| 171 | The number of attempts. |
| 172 | delay : float, default 1 |
| 173 | Initial delay in seconds. |
| 174 | max_delay : float, optional |
| 175 | The max delay between attempts. |
| 176 | backoff : float, default 1 |
| 177 | The multiplier to delay after each attempt. |
| 178 | """ |
| 179 | def decorate(func): |
| 180 | @functools.wraps(func) |
| 181 | def wrapper(*args, **kwargs): |
| 182 | remaining_attempts = attempts |
| 183 | curr_delay = delay |
| 184 | while remaining_attempts > 0: |
| 185 | try: |
| 186 | return func(*args, **kwargs) |
| 187 | except Exception as err: |
| 188 | remaining_attempts -= 1 |
| 189 | last_exception = err |
| 190 | curr_delay *= backoff |
| 191 | if max_delay: |
| 192 | curr_delay = min(curr_delay, max_delay) |
| 193 | time.sleep(curr_delay) |
| 194 | raise last_exception |
| 195 | return wrapper |
| 196 | return decorate |
| 197 | |
| 198 | |
| 199 | @pytest.fixture(scope='session') |
nothing calls this directly
no outgoing calls
no test coverage detected