fuzes
fuzes

Reputation: 2067

how can i pass argument to celery task?

this is my celery task

def task_a(arg1, arg2, arg3, arg4)

and i call the task like following

arg1 is list and arg2,3,4 is integer

task_a.apply_async(arg1, arg2, arg3, arg4)

and i got an error message

    File "/opt/envDjango/lib/python3.5/site-packages/celery/app/task.py", line 518, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
    TypeError: functools.partial object argument after ** must be a mapping, not int

how can i solve the problem??

i tried to make a dictionary and change argument position, they are all failed...

Upvotes: 15

Views: 43265

Answers (2)

Lemayzeur
Lemayzeur

Reputation: 8525

delay will work. This method is convenient as it looks like calling a regular function: see doc here

task_a.delay(*arg,**kwargs)

delay is clearly convenient, but if you want to set additional execution options you have to use apply_async.

task_a.apply_async(args=[arg1, arg2])

Note that the argument passed is a list.

Upvotes: 22

Stefan Lilov
Stefan Lilov

Reputation: 131

To pass arguments to task with apply_async() you need to wrap them in a list and then pass the list as first argument, I.e. apply_async([arg1, arg2, arg3]). See the documentation for more details and examples.

Use delay() as an alternative. The benefit is that it preserves more or less function’s parameters interface, i.e. it is not needed to wrap the args in a list.

Upvotes: 13

Related Questions