APScheduler 使用装饰器添加函数参考

APScheduler add function reference with decorator

我正在尝试添加一个带有装饰器的函数来安排它的执行,但我收到以下错误:

ValueError: This Job cannot be serialized since the reference to its callable (<function inner at 0x7f1c900527d0>) could not be determined. Consider giving a textual reference (module:function name) instead.

我的职能是

@my_decorator
def my_function(id=None):
   print id

我添加如下:

my_scheduler.add_job(function, 'interval',minutes=1)

是否可以添加带有装饰器的函数?有什么想法吗?

作为一种解决方法,我可以定义一个内部定义并调用我的装饰器,但我认为这是一个糟糕的解决方案,我更愿意直接使用它

解决方法:

def outer(id=None):
   @my_decorator
   def my_function(id=None):
      print id

   my_function(id)

my_scheduler.add_job(outer, 'interval',minutes=1)

add_job() 方法接受对您的可调用对象的字符串引用。所以:

my_scheduler.add_job('the.module:my_function', 'interval', minutes=1)

经过反复试验,我成功地完成了我想做的事情:

from functools import wraps

def my_decorator():
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # do stuff
        return wrapper
    return decorator

现在当触发器被apsScheduler触发时调用装饰器

问题在于,我们使用 @wraps 处理天真的内省,即我们更新包装函数,使其看起来像包装函数