Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm not really understanding what the problem is here. There are no issues with my app, but celery can't seem to find it no matter what I try and change.

Here is my directory structure:

django
 / mysite
     __init__.py
     celery.py
     settings.py
     urls.py
     wsgi.py
     / myapp
       admin.py
       apps.py
       models.py
       tasks.py
       urls.py
       views.py
   manage.py

celery.py:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('myapp')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Command: celery -A myapp worker -l info

First command result: AttributeError: module 'celery' has no attribute 'celery' Second command result: ModuleNotFoundError: No module named 'myapp' – dcolumbus Apr 27, 2017 at 22:52

Make sure that your init.py file contains the following:

from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app. from .celery_worker import app as celery_app
__all__ = ['celery_app']

Also, make sure that your celery.py looks something along the lines of this

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from celery.schedules import crontab
import mysite
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('wellfie')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Make sure that your tasks are properly recognized as well: for instance a shared task would look like

@shared_task(name="mysite.myapp.tasks.simple_task") Edit.

Actually you need to move the myapp out of mysite and into the Django folder. I would rename the Django folder to mysite as well, so that you have the parent folder mysite, child folder mysite, that holds wsgi, celery, settings etc, child folder myapp

@dcolumbus actually you need to move the myapp out of mysite and into the Django folder. I would rename the Django folder to mysite as well, so that you have the parent folder mysite, child folder mysite, that holds wsgi, celery, settings etc, child folder myall – Borko Kovacev Apr 28, 2017 at 8:16 [2017-05-08 21:53:56,629: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused. Trying again in 4.00 seconds... – dcolumbus May 8, 2017 at 21:54

Your celery.py file should be in myapp folder. (http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html)

You should run celery -A myapp worker -l info in mysite folder.

[2017-05-08 21:53:56,629: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61] Connection refused. Trying again in 4.00 seconds... – dcolumbus May 8, 2017 at 21:54

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.