Celery任务on_success处理程序的kwargs始终为空

时间:2019-07-14 10:22:33

标签: python-2.7 celery celery-task kwargs

Celery版本= 4.3.0

我要做的就是能够访问任务自定义成功处理程序中celery任务函数的实际参数(常规和关键字)。我能够正确打印出非关键字参数,但是关键字参数始终为空。

下面是代码

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正常但关键字arg为空?

现在我有解决方法来完成工作,例如在任务中仅使用args而不是kwargs并在 on_success 中访问它们,但是我真的很想知道上面的问题。

1 个答案:

答案 0 :(得分:0)

您的任务签名需要更改为def on_success(self, retval, task_id, *args, **kwargs):