简体   繁体   中英

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 adition, you can add some aditional data necesaries 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.

Backend is configured in settings module as:

CELERY_RESULT_BACKEND = 'django-db' # in this case it is django DB

If you configured django DB as a backend then you could import it as

from django-celery-results.models import TaskResult

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM