Celery获取已注册任务的列表 [英] Celery Get List Of Registered Tasks

查看:65
本文介绍了Celery获取已注册任务的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取已注册任务的列表?

Is there a way to get a list of registered tasks?

我尝试过:

celery_app.tasks.keys()

仅返回内置的Celery任务,例如celery.chord,celery.chain等.

Which only returns built in Celery tasks like celery.chord, celery.chain etc.

推荐答案

对于较新版本的celery(4.0或更高版本),我们可以按以下方式获取已注册的任务.

For the newer versions of celery(4.0 or above), we can get registered tasks as follows.

from celery import current_app 
tasks = current_app.tasks.keys()

对于较旧版本的芹菜,芹菜<4,我们可以得到如下已注册的任务.

For older versions of celery, celery < 4, we can get registered tasks as follows.

from celery.task.control import  inspect
i = inspect()
i.registered_tasks()

这将给出所有工人和工人的字典.相关的已注册任务.

This will give a dictionary of all workers & related registered tasks.

from itertools import chain
set(chain.from_iterable( i.registered_tasks().values() ))

如果您有多个工作人员执行相同的任务,或者仅需要一组所有已注册的任务,那么它将执行此工作.

In case if you have multiple workers running same tasks or if you just need a set of all registered tasks, it does the job.

替代方式:

从终端可以使用此命令转储已注册的任务

From terminal you can get a dump of registered tasks by using this command

celery inspect registered

要检查与特定应用相关的任务,您可以传递应用名称

To inspect tasks related to a specific app, you can pass app name

celery -A app_name inspect registered

这篇关于Celery获取已注册任务的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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