File size: 1,032 Bytes
e6583bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 time
import functools

def retry(exceptions, tries=3, delay=2, backoff=2):
    """

    Retry decorator with exponential backoff.



    Args:

        exceptions (tuple): exceptions to catch and retry on.

        tries (int): number of attempts.

        delay (int): initial delay between retries.

        backoff (int): multiplier for delay.



    Usage:

        @retry((TimeoutError, SomeOtherError))

        def func(...):

            ...

    """
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            _tries, _delay = tries, delay
            while _tries > 1:
                try:
                    return func(*args, **kwargs)
                except exceptions as e:
                    print(f"Warning: {e}, retrying in {_delay} seconds...")
                    time.sleep(_delay)
                    _tries -= 1
                    _delay *= backoff
            return func(*args, **kwargs)
        return wrapper
    return decorator