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