I have a Django 1.7 project with Celery 3.1. All the apps in my Django project work with the new AppConfig. The problem is that not all the tasks are found with autodiscover_tasks
:
app.autodiscover_tasks(settings.INSTALLED_APPS)
If i use autodiscover_tasks like this it wil work:
app.autodiscover_tasks(settings.INSTALLED_APPS + ('apps.core','apps.sales'))
The tasks defined in websites are found but the tasks in core and sales are not. All have the same layout with apps.py
and tasks.py
.
The project folder structure is:
apps
core
apps.py
tasks.py
dashboard
apps.py
sales
apps.py
tasks.py
websites
apps.py
tasks.py
The class definitions are as follows:
class WebsitesConfig(AppConfig):
name = 'apps.websites'
verbose_name = 'Websites'
class SalesConfig(AppConfig):
name = 'apps.sales'
verbose_name = 'Sales'
This is discussed in a number of Celery issues, such as #2596 and #2597.
If you are using Celery 3.x, the fix is to use:
from django.apps import apps
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])
As mentioned in #3341, if you are using Celery 4.x (soon to be released) you can use:
app.autodiscover_tasks()
I just had this problem because of a misconfigured virtual environment.
If an installed app has a dependency missing from the virtual environment in which you're running celery, then the installed app's tasks will not be auto discovered. This hit me as I was moving from running my web server and celery on the same machine to a distributed solution. A bad build resulted in different environment files on different nodes.
I added the dependencies that were missing then restarted the celery service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With