Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the result of a delay-call using celery in a django view?

I`ve followed the guidelines in http://celeryq.org/docs/django-celery/getting-started/first-steps-with-django.html and created a view that calls my test method in tasks.py:

import time
from celery.decorators import task

@task()
def add(x, y):
    time.sleep(10)
    return x + y

But if my add-method takes a long time to respond, how can I store the result-object I got when calling add.delay(1,2) and use it to check the progress/success/result using get later?

like image 402
Weholt Avatar asked Sep 13 '10 18:09

Weholt


People also ask

What is delay in Django Celery?

delay(*args, **kwargs) Shortcut to send a task message, but doesn't support execution options. calling ( __call__ ) Applying an object supporting the calling API (e.g., add(2, 2) ) means that the task will not be executed by a worker, but in the current process instead (a message won't be sent).

What does delay do in Celery?

delay() is the quickest way to send a task message to Celery. This method is a shortcut to the more powerful . apply_async() , which additionally supports execution options for fine-tuning your task message.

How do you use Celery in Django?

Here are some key points: DJANGO_SETTINGS_MODULE must be set in the environment before starting a Celery process. Its presence in the environment triggers internal magic in Celery to run the Django setup at the right time. The Celery "application" must be created and configured during startup of both Django and Celery.

What is .delay in Django?

delay is a sort of handle to the background task.


1 Answers

You only need the task-id:

result = add.delay(2, 2)
result.task_id

With this you can poll the status of the task (using e.g. AJAX) Django-celery comes with a view that returns results and status in JSON: http://celeryq.org/docs/django-celery/reference/djcelery.views.html

like image 106
asksol Avatar answered Oct 15 '22 06:10

asksol