0

I am developing an application where i have to periodically call a function which is in myapp/task.py from the celery.py file which is located in my project directory.But when I do an import statement from the celery.py file like

from myapp.tasks import test

I get the following error which I am not able to resolve:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Here is the code from init.py:

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

 __all__ = ['celery_app']

The Code from celery.py

from __future__ import absolute_import,unicode_literals
import os
from celery import Celery
from celery.schedules import crontab
from myapp.tasks import test

os.environ.setdefault('DJANGO_SETTINGS_MODULE','project.settings') app=Celery('project')

app.config_from_object('django.conf:settings',namespace='CELERY')

app.autodiscover_tasks()

@app.on_after_configure.connect

def setup_monitor_call(sender,**kwargs):

   sender.add_periodic_task(30.0,test.s(),name='call test every 30 seconds')

Now in my app directory there is a file named tasks.py which has the test method which i am calling from celery.py:

from __future__ import absolute_import, unicode_literals
from celery.decorators import task
from celery import shared_task
from celery.utils.log import get_task_logger

logger=get_task_logger(__name__)

def test():
  print("Hello Celery")

Now the exact problem is that when i write the following import statement in the celery.py file:

from myapp.tasks import test

and then when i run python manage.py runserver i get the error:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

The moment i remove the import statement, the server runs correctly. I cannot figure out why?

2
  • It's hard to tell without seeing at least some of the code in both modules. Commented May 25, 2017 at 23:36
  • I have now added the source code Commented May 27, 2017 at 0:24

1 Answer 1

0

This error is due to an incompatibility with that import, for example a cyclic import. I have recently worked on a project with celery and sometimes imports are as fragile as a house of cards.
I recommend that you make the calls to the tasks as follows in this example:

.....otherimports
from celery import Celery

import incidencias.celery_conf as celery_conf

app = Celery('myname')
app.config_from_object(celery_conf, namespace='CELERY')
app.conf.timezone = 'Europe/Madrid'

def some_def():
     code code code code....  
     app.send_task('myapp.tasks.test', args=[whatyouneed.ifneed])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.