The number of python black magic exception retries, the number of decorators in the interval

origin:
   Find a library retrying on github
   But this library is not easy to use at all, it has not been maintained for a long time
   simply, make a
   You can also extend the timeout
from functools import wraps
from threading import Event


def retry_exception(retry_count=0, interval_wait=0):
    def wrap(f):
        @wraps(f)
        def func(*args, **kwargs):
            try:
                return f(*args, **kwargs)
            except Exception as e:
                if retry_count == 0:
                    return str(e)
                if retry_count >= 1:
                    count = retry_count
                    while 1:
                        Event().wait(interval_wait)
                        try:
                            count = count - 1
                            return f(*args, **kwargs)
                        except Exception as e:
                            if count == 0:
                                return str(e)
                            continue
        return func
    return wrap


@retry_exception(retry_count=3, interval_wait=3)
def tt():
    a = 1
    if a != 2:
        raise Exception('i am exception')
    print(a)

print(tt()) 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326596013&siteId=291194637