Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i pass argument to celery task?

Tags:

django

celery

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...

like image 629
fuzes Avatar asked May 09 '18 04:05

fuzes


People also ask

Can Celery tasks be async?

Celery is a task queue/job queue based on asynchronous message passing. It can be used as a background task processor for your application in which you dump your tasks to execute in the background or at any given moment. It can be configured to execute your tasks synchronously or asynchronously.

How does Celery execute tasks?

It allows you to offload work from your Python app. Once you integrate Celery into your app, you can send time-intensive tasks to Celery's task queue. That way, your web app can continue to respond quickly to users while Celery completes expensive operations asynchronously in the background.

What is bind in Celery task?

Bound tasks A task being bound means the first argument to the task will always be the task instance ( self ), just like Python bound methods: logger = get_task_logger(__name__) @app. task(bind=True) def add(self, x, y): logger.

What happens when a Celery task fails?

Celery will stop retrying after 7 failed attempts and raise an exception.


2 Answers

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.

like image 77
Lemayzeur Avatar answered Oct 06 '22 07:10

Lemayzeur


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.

like image 31
Stefan Lilov Avatar answered Oct 06 '22 09:10

Stefan Lilov