10

如果我像这样创建一个 python 装饰器函数

def retry_until_true(tries, delay=60):
    """
    Decorator to rety a function or method until it returns True.
    """
    def deco_retry(f):
        def f_retry(*args, **kwargs):
            mtries  = tries
            rv = f(*args, **kwargs)
            while mtries > 0:
                if rv is True:
                    return True
                mtries -= 1
                time.sleep(delay)
                rv = f(*args, **kwargs)
            return False
        return f_retry
    return deco_retry

我可以这样使用

    @retry_until_true(20, delay=30)
    def check_something_function(x, y):
        ...
        return True

但是有没有办法在运行时将“尝试”和“延迟”的不同值传递给装饰器,以便 20 和 30 是变量?

4

2 回答 2

11

你可以使用一个类作为装饰器,实例变量为triesand delay

class RetryUntilTrue(object):
    def __init__(self, f=None, tries=10, delay=30):
        self.f = f
        self.tries = tries
        self.delay = delay

    def __call__(self, *args, **kwargs):
        if self.f is None:
            self.f = args[0]
        else:
            tries = self.tries
            while tries:
                if self.f(*args, **kwargs):
                    return True

                tries -= 1
                time.sleep(self.delay)

用法:

@RetryUntilTrue
def foo(x):
    pass

foo.tries = 20
foo.delay = 1

@RetryUntilTrue(tries=100, delay=9999)
def bar(x):
    pass

bar.tries -= 1
bar.delay = foo.delay
于 2012-08-07T23:36:23.617 回答
10

当然可以,只需将函数定义嵌套在另一个函数中,例如:

def explicit_setup_func(tries, delay=60):
    @retry_until_true(tries, delay)
    def check_something_function(x, y):
        # Code

但是,类装饰器解决方案更实用。

于 2012-08-07T23:42:29.737 回答