11

I am using Celery + RabbitMQ for queuing tasks in my Django App,

I want to track the state of a task using the task_id and the task_state.

For that i created a TaskModel(Model) to store the task_id, task_state and some additional data in the database. On task execution, a new TaskModel object is save and updated as the task progresses. Everything is working fine.

However, i still need to add a lot of functionality and features and error protections etc. That's when i remembered the celery documentation mentions the django-celery-results.

So i followed the django-celery-results documentation instructions. Tasks results get stored in the default django database in a dedicated table, However only after the task concludes... and not during the PENDING, STARTED states.

Is it possible to use django-celery-results to store and query tasks during the PENDING and STARTED states? or not?

Thanks

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

6 Comments

task_result = TaskResult.objects.get(self.request.id) what is self in this line?
Is it an error? Why do you use self outside any class?
Hello, you are partly right, the function definition should declare self, otherwise it will produce an error. When bind is set to True (bind=True), in the decorator @shared_task. self is references the current task instance. I am editing the function definition accordingly.
In fact no, it did't, don't know why but TaskReults are always pending and have no results, it must be my fault, but i don't know where
Is your task running and displaying output? Make sure you store the task_result using task_result.save() every time you update its value. The entry in the database will not be updated unless you force the model to update using .save()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.