Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get celery results model (using django-celery-results) within task

I'm planning on using django-celery-results backend to track status and results of Celery tasks.

Is the django-celery-results backend suitable to store status of task while it is running, or only after it has finished?

It's not clear when the TaskResult model is first created (upon task creation, task execution, or completion?)

If it's created upon task creation, will the model status automatically be updated to RUNNING when the task is picked up, if task_track_started option is set?

Can the TaskResult instance be accessed within the task function?

Another question here appears to indicate so but doesn't mention task status update to RUNNING

like image 839
Finn Andersen Avatar asked May 08 '19 05:05

Finn Andersen


People also ask

How do you use Celery beat in Django?

To use the Celery Beat, we need to configure the Redis server in the Django projects settings.py file. As we have installed the Redis server on the local machine, we will point the URL to localhost. The CELERY_TIMEZONE variable must be correctly set to run the tasks at the intended times.

What is result backend in Celery?

Celery uses a result backend to keep track of the tasks' states. In the previous tutorial, we saw how Celery works and how to integrate it into a Django application. In this tutorial, we are going to use the RPC (RabbitMQ/AMQP) result backend to store and retrieve the states of tasks.


1 Answers

About Taskresult, of course, you can access during execution task, you only need import:

from django_celery_results.models import TaskResult

With this you can access to model taskresult filter by task_id, task_name etc, This is official code, you can filter by any of this fields https://github.com/celery/django-celery-results/blob/master/django_celery_results/models.py

Example:

task = TaskResult.objects.filter(task_name=task_name, status='SUCCESS').first()

In addition, you can add some additional data necessaries for your task in models fields. When task is created and is in execution also is posible modify fields but in case that status is 'SUCCEES', you only can read it, status task is auto save by default.

Example:

task_result.meta = json.dumps({'help_text': {'date': '2020-11-30'}})

I recommend work with is_dict() function, here you can watch models attributes.

like image 199
Juan José Monsalve Avatar answered Sep 29 '22 17:09

Juan José Monsalve