[Solved]-Is it possible to query state of a celery tasks using django-celery-results during the execution of a task?

5👍

After reviewing the source code of django-celery-result it turns out the code is pretty simple and straight-forward.

In order to use django-celery-result to store tasks after the task function is called use the following:

from django_celery_results.models import TaskResult
import json

@shared_task(bind=True)
def foo(self, count):
 print('hello')
 task_result = TaskResult.objects.get(self.request.id)
 counter = 1
 interval = 20 #Limit updates to reduce database queries
 interval_count = count/interval
 for i in range(count):
  print(i)
  if counter>= interval_count:
   interval_count+=count/interval
   task_result.meta = json.dumps({'progress': counter/count})
   task_result.save()
  counter+=1
 task_result.save()
 return

def goo()
 task = foo.delay(1000)
 task_result = TaskResult(task_id=task.task_id)
 task_result.save()
👤Ouss

Leave a comment