Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register celery tasks across apps/projects in Django?

How to I call a celery (app) task from the project level and vice versa.

I have a Django directory structure like:

- proj
     |_ celery.py
     |_ settings.py
- app
     |_ tasks.py
     |_ views.py

In settings.py I have a CELERYBEAT_SCHEDULE var which refers to a shared_task in app/tasks. This task is not listed in celery inspect registered.

Neither are tasks which are listed in the registry since they were defined in proj/celery.py, callable by functions in apps/views.py.

I've followed the instructions on the celery website.

like image 623
J0hnG4lt Avatar asked May 23 '16 12:05

J0hnG4lt


1 Answers

Functions in the shared_task decorator must be listed in the apps AppConfig.

Under app/apps.py import your task like so:

from .tasks import add

In app/tasks.py import shared_task like so:

from proj.celery import shared_task

In proj/celery.py import shared_task like so:

import celery
from celery import shared_task

Pycharm will complain about unused imports, however they are used, just not in the file pycharm is parsing.

Now when I start celery worker like:

python manage.py celeryd --loglevel=DEBUG &

rather than

celery -A pears worker --loglevel=info &

My tasks in app/tasks.py are registered and callable.

like image 161
J0hnG4lt Avatar answered Oct 07 '22 22:10

J0hnG4lt