Celery Get List Of Registered Tasks

2020-05-31 05:27发布

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.

标签: python celery
3条回答
啃猪蹄的小仙女
2楼-- · 2020-05-31 05:35
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
查看更多
家丑人穷心不美
3楼-- · 2020-05-31 05:36

With the newer versions of celery ( 4.0 and above ), the following seems to be the right way:

 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
查看更多
叼着烟拽天下
4楼-- · 2020-05-31 05:49

In a shell, try:

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

current_app.tasks has all tasks available as a dictionary. The keys are all the names of registered tasks in the current celery app you are running.

查看更多
登录 后发表回答