Celery任务功能的自定义属性 [英] Celery task function custom attributes
问题描述
我有一个像这样的芹菜任务功能-
I have a celery task function that looks like this-
@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看起来像这样
and my_custom_decorator looks like this
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()
?
I see that this is because the actual callable object that is invoked to execute the task is an object of type celery task class.
How can I retain my attribute 'ltc' on this object so it can be accessed from within the task as show above i.e - my_task.ltc.some_func()
?
谢谢
推荐答案
我认为一种简单的方法是引入 ltc
作为关键字参数.
I think one easy way to do that would be to introduce ltc
as a keyword parameter..
@task(base=MyBaseTask)
@my_custom_decorator
def my_task(*args, **kwargs):
ltc = kwargs['ltc']
ltc.some_func()
也许是这样的:
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
kwargs['ltc'] = ltc
ret_obj = f(*args, **kwargs)
except Exception, e:
raise
return ret_obj
_inner.ltc = ltc
return _inner
我不知道是否有芹菜的任务处理方式.希望对您有帮助.
I don't know if there's a celery's task way to do this. Hope this helps you.
这篇关于Celery任务功能的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!