1

我想编写一个python装饰器,以便再次运行引发异常的函数,直到它成功,或者在放弃之前达到最大尝试次数。

像这样:

def tryagain(func):
    def retrier(*args,**kwargs,attempts=MAXIMUM):
        try:
            return func(*args,**kwargs)
        except Exception as e:
            if numberofattempts > 0:
                logging.error("Failed. Trying again")
                return retrier(*args,**kwargs,attempts=attempts-1)
            else:
                logging.error("Tried %d times and failed, giving up" % MAXIMUM)
                raise e
    return retrier

我的问题是我想要保证无论 kwargs 包含什么名称,都不会与用于表示尝试次数的名称发生冲突。

attempts但是,当函数本身作为关键字参数时,这不起作用

@tryagain
def other(a,b,attempts=c):
    ...
    raise Exception

other(x,y,attempts=z)

在此示例中,如果运行 other,它将运行 z 次而不是 MAXIMUM 次(请注意,要发生此错误,必须在调用中显式使用关键字参数!)。

4

2 回答 2

3

您可以指定装饰器参数,类似于以下内容:

import logging

MAXIMUM = 5

def tryagain(attempts=MAXIMUM):
    def __retrier(func):
        def retrier(*args,**kwargs):
            nonlocal attempts
            while True:
                try:
                    return func(*args,**kwargs)
                except Exception as e:
                    attempts -= 1
                    if attempts > 0:
                        print('Failed, attempts left=', attempts)
                        continue
                    else:
                        print('Giving up')
                        raise
        return retrier
    return __retrier


@tryagain(5)                              # <-- this specifies number of attempts
def fun(attempts='This is my parameter'): # <-- here the function specifies its own `attempts` parameter, unrelated to decorator
    raise Exception(attempts)

fun()
于 2019-12-14T21:10:35.677 回答
1

从函数属性获取重试尝试次数,而不是参数。

def tryagain(func):
    def retrier(*args,**kwargs):
        retries = getattr(func, "attempts", MAXIMUM)
        while retries + 1 > 0:
            try:
                return func(*args, **kwargs)
            except Exception as e:
                logging.error("Failed. Trying again")
                last_exception = e
            retries -= 1
        else:
            logging.error("Tried %d times and failed, giving up", retries)
            raise last_exception

    return retrier

@tryagain
def my_func(...):
    ...

my_func.attempts = 10
my_func()  # Will try it 10 times

要制作MAXIMUM可以在调用 decorate 函数时指定的内容,请将定义更改为

def tryagain(maximum=10):
    def _(f):
        def retrier(*args, **kwargs):
            retries = getattr(func, "attempts", maximum)
            while retries + 1 > 0:
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    logging.error("Failed. Trying again")
                    last_exception = e
                retries -= 1
            else:
                logging.error("Tried %d times and failed, giving up", retries)
                raise last_exception
        return retrier
    return _

尽管仍然存在与属性发生名称冲突的风险,attempts但很少使用函数属性这一事实使得记录tryagain不使用具有预先存在的attempts属性的函数更合理。

(我把它作为一个练习来修改tryagain以将属性名称用作参数:

@tryagain(15, 'max_retries')
def my_func(...):
    ...

这样您就可以在装饰时选择一个未使用的名称。就此而言,您还可以使用参数 totryagain作为要添加到的关键字参数的名称my_func。)

于 2019-12-14T21:18:07.007 回答