Celery AppRegistryNotReady: Apps aren't loaded yet when using Django with Celery
I'm encountering an issue with Celery and Django where I receive an "AppRegistryNotReady" exception, stating that "Apps aren't loaded yet." This occurs when attempting to run Celery with the beat scheduler.
Here are the key details:
- I'm using Django with Celery for background task processing.
- When trying to start the Celery beat scheduler using the command:
celery -A myproject beat -l info
I encounter the following traceback:
Traceback (most recent call last): ... raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
- The issue seems related to Django settings or app loading during Celery initialization.
This is my code
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE','myproject.settings')
app = Celery('myproject')
app.conf.enable_utc = False
app.conf.update(timezone = 'Asia/Kolkata')
app.config_from_object(settings, namespace = 'CELERY')
# celery beat settings
app.conf.beat_schedule ={
}
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
settings.py
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE ='Asia/Kolkata'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERY_RESULT_BACKEND = 'django-db'
from myapp.models import Client,ImportSettings
task_intervals = ImportSettings.objects.all()
CELERY_BEAT_SCHEDULE = {}
for task_interval in task_intervals:
interval_seconds = task_interval.check_frequency_minutes
client_id = task_interval.client
CELERY_BEAT_SCHEDULE[f'import-{client_id}'] = {
'task': 'myapp.tasks.process_csv_files',
'schedule': interval_seconds,
'args': (
'directory_path',
'table1',
'table2'
)
}
__init__.py
from .celery import app as celery_app
__all__ = ('celery_app',)
Any insights or suggestions on how to resolve this issue would be greatly appreciated. Thanks!
Tried specifying the Celery app with the full path:
celery -A myproject.celery beat -l info
Tried starting Celery beat with the --detach option:
celery -A myproject.celery beat --detach -l info
Checked Django settings and app loading:
Verified that Django settings and app loading are correct in myproject/settings.py and myproject/__init__.py
Ensured that Celery is configured correctly in the myproject/celery.py file
Checked for any issues in the myapp/models.py file, especially around the Import settings model.
Confirmed that the virtual environment is activated before running Celery commands.
created an entire new project and still the error persist