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?
Celery maintains a registry of all tasks. This is how the worker can lookup a task by name when it receives a task message:
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:
NOTE:
import_default_modulesdoesn’t exist before Celery 2.5, back then you’d have todo:
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.:
will set the ‘loglevel’ and ‘logger’ attributes of all tasks.
Alternatively you can use a common base class: