我有一个看起来像这样的芹菜任务功能-
@task(base=MyBaseTask)
@my_custom_decorator
def my_task(*args, **kwargs):
my_task.ltc.some_func() #fails - attribute ltc doesn't exist on the object
my_custom_decorator 看起来像这样
def my_custom_decorator (f):
from functools import wraps
ltc = SomeClass()
@wraps(f)
def _inner(*args, **kwargs):
ret_obj = None
try:
f.task_cache = ltc
ret_obj = f(*args, **kwargs)
except Exception, e:
raise
return ret_obj
_inner.ltc = ltc
return _inner
我看到这是因为调用来执行任务的实际可调用对象是 celery 任务类类型的对象。如何在此对象上保留我的属性“ltc”,以便可以从任务中访问它,如上图所示 - my_task.ltc.some_func()
?
谢谢,