Python3 AOP实现接口重试

python-aop简单回顾

def aop(func):
    def wrapped():
        print("before do something")
        return  func()
        print("after do something")
    return wrapped
 

@aop
def func():
    print(1+2)
#带参数装饰器

    #handle args
    def __retry(self, expect_status=200):
        """
        :return:
        """
        def wrapped(func):
            #handle function
            def run(*args, **kwargs):
                retry = 0
                response = func(*args, **kwargs)
                while status!=expect_status and response["message"] == []:
                    time.sleep(self.sleep_time)
                    retry += 1
                    response = func(*args, **kwargs)
                    if retry >= 3:
                        break
                    status=response.status_code

            return run

        return wrapped

你可能感兴趣的:(前端,服务器,linux)