Skip to content Skip to sidebar Skip to footer

How To Get All Tasks And Periodic Tasks In Celery

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

Solution 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:

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

Post a Comment for "How To Get All Tasks And Periodic Tasks In Celery"