| 150 | |
| 151 | |
| 152 | def retry( |
| 153 | exceptions: Any = Exception, retries: int = 20, delay: int = 1 |
| 154 | ) -> Callable[[Callable[..., Any]], Callable[..., Any]]: |
| 155 | def decorator(f: Callable[..., Any]) -> Callable[..., Any]: |
| 156 | def _retry(*args: Any, **kwargs: Any) -> Any: |
| 157 | _tries = retries |
| 158 | while _tries > 1: |
| 159 | try: |
| 160 | logger.debug("{} {} attempt(s) left.".format(f, _tries - 1)) |
| 161 | return f(*args, **kwargs) |
| 162 | except exceptions: |
| 163 | time.sleep(delay) |
| 164 | _tries -= 1 |
| 165 | logger.warning("{} has failed after {} tries".format(f, retries)) |
| 166 | return f(*args, **kwargs) |
| 167 | |
| 168 | return _retry |
| 169 | |
| 170 | return decorator |
| 171 | |
| 172 | |
| 173 | def http_req( |