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?
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).
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.
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.
delay is a sort of handle to the background task.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With