在执行任务期间是否可以使用django-celery-results查询celery任务的状态? [英] Is it possible to query state of a celery tasks using django-celery-results during the execution of a task?

查看:583
本文介绍了在执行任务期间是否可以使用django-celery-results查询celery任务的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Celery + RabbitMQ在我的Django应用程序中排队任务,



我想使用task_id和task_state跟踪任务的状态。

为此,我创建了一个TaskModel(Model)来将task_id,task_state和一些其他数据存储在数据库中。在任务执行时,将保存一个新的TaskModel对象,并随着任务的进行更新。一切正常。



但是,我仍然需要添加很多功能和特性以及错误保护等。那时候我还记得芹菜文档中提到了django-celery-results。 p>

所以我遵循了django-celery-results文档说明。任务结果仅在任务结束后才存储在默认django数据库的专用表中,但是 ...而不是在PENDING,STARTED状态期间存储。



在PENDING和STARTED状态期间是否可以使用django-celery-results存储和查询任务?或不?



谢谢

解决方案

在查看django-celery的源代码之后-result证明代码非常简单明了。



为了在任务功能被调用后使用django-celery-result存储任务,请使用以下:

  from django_celery_results.models import TaskResult 
import json

@shared_task(bind = True)
def foo(自身,计数):
print('hello')
task_result = TaskResult.objects.get(self.request.id)
counter = 1
interval = 20#限制更新以减少数据库查询
interval_count =计数/间隔
for i在range(count):
print(i)
如果counter> = interval_count:
interval_count + =计数/间隔
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()


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

解决方案

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()

这篇关于在执行任务期间是否可以使用django-celery-results查询celery任务的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆