Django Celery获取任务计数 [英] Django Celery get task count
问题描述
我当前正在将芹菜与django结合使用,并且一切正常。
I am currently using django with celery and everything works fine.
但是,如果服务器是服务器,我希望能够为用户提供取消任务的机会通过检查当前排定的任务超载。
However I want to be able to give the users an opportunity to cancel a task if the server is overloaded by checking how many tasks are currently scheduled.
如何实现此目标?
我正在使用Redis作为经纪人。
I am using redis as broker.
我刚刚发现了这个:
在Celery中检索队列中的任务列表
这在某种程度上与我的问题有关,但我不知道需要列出任务,只需对它们进行计数即可:)
It is somehow relate to my issue but I don't need to list the tasks , just count them :)
推荐答案
如果您的代理配置为 redis: // localhost:6379/1
,然后将您的任务提交到普通的 celery
队列中,然后可以通过以下方式获取长度:
If your broker is configured as redis://localhost:6379/1
, and your tasks are submitted to the general celery
queue, then you can get the length by the following means:
import redis
queue_name = "celery"
client = redis.Redis(host="localhost", port=6379, db=1)
length = client.llen(queue_name)
或者,从shell脚本(适用于监视器等):
Or, from a shell script (good for monitors and such):
$ redis-cli -n 1 -h localhost -p 6379 llen celery
这篇关于Django Celery获取任务计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!