Python Celery 获取已注册任务列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26058156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Celery Get List Of Registered Tasks
提问by Richard Knop
Is there a way to get a list of registered tasks?
有没有办法获取已注册任务的列表?
I tried:
我试过:
celery_app.tasks.keys()
Which only returns built in Celery tasks like celery.chord, celery.chain etc.
只返回内置的 Celery 任务,如 celery.chord、celery.chain 等。
回答by ChillarAnand
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.
如果您有多个工作人员运行相同的任务,或者您只需要一组所有已注册的任务,它就会完成工作。
Alternate Way:
替代方式:
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
回答by Mike - kentivo
In a shell, try:
在 shell 中,尝试:
from celery import current_app
print(current_app.tasks.keys())
current_app.taskshas all tasks available as a dictionary. The keys are all the names of registered tasks in the current celery app you are running.
current_app.tasks将所有任务作为字典提供。键是您正在运行的当前 celery 应用程序中所有已注册任务的名称。
回答by Naved Khan
With the newer versions of celery ( 4.0 and above ), the following seems to be the right way:
使用较新版本的 celery(4.0 及更高版本),以下似乎是正确的方法:
from celery import current_app
_ = current_app.loader.import_default_modules()
tasks = list(sorted(name for name in current_app.tasks
if not name.startswith('celery.')))
return tasks

