芹菜版本 = 4.3.0
我想做的只是能够在任务的自定义成功处理程序中访问芹菜任务函数(正常和关键字(的实际参数。我能够正确打印出非关键字参数,但关键字参数始终为空。
下面是代码
taskResultsHandler
from celery import Task
class ActionOnCompletion(Task):
def on_success(self, retval, task_id, args, kwargs):
print "inside success handler"
print retval[0] # prints => ["results"]
print retval[1].get('meta') # prints => this is meta
print "task function args were"
print args # prints => (<value_of_arg1 as passed while invoking the task>, <value_of_arg2 as passed while invoking the task>)
print "keyword args"
print type(kwargs) # prints => <type 'dict'>
print kwargs # prints => {} I was expecting {"someKeyWordArg":"randommmm"} to be printed here !
print kwargs.get('someKeyWordArg') # does not print anything
def on_failure(self, exc, task_id, args, kwargs, einfo):
print einfno
实际任务
from celeryConfig.celeryApp import celapp
from celeryConfig.taskResultsHandler import ActionOnCompletion
@celapp.task(base=ActionOnCompletion)
def myTask(arg1, arg2, someKeyWordArg="randommmm"):
print("starting my task")
print someKeyWordArg
time.sleep(5)
return ["results"], {'meta':"this is meta"}
有人可以帮我了解我在上面缺少什么,为什么我能够得到非关键字参数很好,但关键字参数是空的?
现在我有解决方法来完成我的工作,比如在我的任务中只使用 args 而不是 kwargs 并在on_success中访问它们,但我真的很想知道上面出了什么问题。
您的任务签名需要更改为def on_success(self, retval, task_id, *args, **kwargs):