How to get all tasks and periodic tasks in Celery [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-04 12:55:38

问题


Possible Duplicate:
How can I find all subclasses of a given class in Python?

In my Django project, I have some subclass of Celery's Task and PeriodicTask:

class CustomTask(Task):
    # stuff

class CustomPeriodicTask(PeriodicTask):
    # stuff

I need all Task classes to add some custom logging configuration. So I thought I can us __subclasses__, but this does not work:

>>> Task.__subclasses__()
[<unbound PeriodicTask>, <class handle_register of <Celery default:0xa1cc3cc>>]

Is it somehow possible to get all my Task and PeriodicTask subclasses in a dynamic way?


回答1:


Celery maintains a registry of all tasks. This is how the worker can lookup a task by name when it receives a task message:

from celery import current_app
all_task_names = current_app.tasks.keys()
all_tasks = current_app.tasks.values()
foo_task = current_app.tasks['tasks.foo']

all_task_classes = [type(task) for task in current_app.tasks.itervalues()]

The task registry is only populated as the modules containing tasks are imported. If you have not imported all modules you can do like the celery worker does, and import all configured task module sources:

current_app.loader.import_default_modules()

NOTE: import_default_modules doesn't exist before Celery 2.5, back then you'd have to do:

from celery.loaders import current_loader
current_loader().init_worker()

NOTE2: Are you sure you want to edit all task classes like this? Celery comes with a number of ways to configure task classes, e.g.:

CELERY_ANNOTATIONS = {
     '*': {
         'loglevel': logging.DEBUG,
         'logger': logging.getLogger('foo'),
     },
}

will set the 'loglevel' and 'logger' attributes of all tasks. Alternatively you can use a common base class:

class MyTask(Task):
    abstract = True   # means this base task won't be registered
    loglevel = logging.DEBUG
    logger = logging.getLogger('foo'),


来源:https://stackoverflow.com/questions/12651872/how-to-get-all-tasks-and-periodic-tasks-in-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!