61

I'm trying to set up Django-Celery. I'm going through the tutorial

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

when I run $ python manage.py celery worker --loglevel=info

I get

[Tasks]


/Users/msmith/Documents/dj/venv/lib/python2.7/site-packages/djcelery/loaders.py:133:     UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in     production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '

[2013-08-08 11:15:25,368: WARNING/MainProcess] /Users/msmith/Documents/dj/venv/lib/python2.7/site-packages/djcelery/loaders.py:133: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '

[2013-08-08 11:15:25,369: WARNING/MainProcess] celery@sfo-mpmgr ready.
[2013-08-08 11:15:25,382: ERROR/MainProcess] consumer: Cannot connect to     amqp://guest@127.0.0.1:5672/celeryvhost: [Errno 61] Connection refused.
Trying again in 2.00 seconds...

has anyone encountered this issue before?

settings.py

# Django settings for summertime project.
import djcelery
djcelery.setup_loader()

BROKER_URL = 'amqp://guest:guest@localhost:5672/'

...

INSTALLED_APPS = {
    ...
    'djcelery',
    'celerytest'
}

wsgi.py

import djcelery
djcelery.setup_loader()
IdeoREX
  • 1,455
  • 5
  • 22
  • 39
  • celery need message broker. I think you missed the message broker setting. you need to install rabbitmq. after installation, you need to make vhost, user and set permissions. – Curry Oct 04 '13 at 08:38

8 Answers8

36

Update Jan 2022: This answer is outdated. As suggested in comments, please refer to this link

The problem is that you are trying to connect to a local instance of RabbitMQ. Look at this line in your settings.py

BROKER_URL = 'amqp://guest:guest@localhost:5672/'

If you are working currently on development, you could avoid setting up Rabbit and all the mess around it, and just use a development version of a message queue with the Django database.

Do this by replacing your previous configuration with:

BROKER_URL = 'django://'

...and add this app:

INSTALLED_APPS += ('kombu.transport.django', )

Finally, launch the worker with:

./manage.py celery worker --loglevel=info

Source: http://docs.celeryproject.org/en/latest/getting-started/brokers/django.html

finiteautomata
  • 3,753
  • 4
  • 31
  • 41
  • 3
    Wow, the tutorial completely ignores this bit. – nym Sep 16 '15 at 18:33
  • 12
    The link is outdated – Ren Mar 26 '19 at 07:21
  • 2
    Previous versions of Celery required a separate library to work with Django, but since 3.1 this is no longer the case. Django is supported out of the box now [link](https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-celery-with-django) – Dmytro Manzhula Jan 09 '22 at 13:53
25

I got this error because rabbitmq was not started. If you installed rabbitmq via brew you can start it using brew services start rabbitmq

xiao
  • 1,718
  • 4
  • 23
  • 31
  • Your answer is very important. Yes you can skip the queue but in my experience there are a number of errors created only in the queue system. Additionally, some apps are dependent on polling/similar the service for locking out access (in queue, no edit scenario). I am of the belief that Rabbit should be running "proper" and wanted to add this note as this was what I forgot as I was setting up install documentation for an app. Thanks. – Marc Jun 28 '18 at 19:17
  • I just had an interesting failure on Rabbit. Apparently Elixir was auto-starting on local. As such my Rabbit port was unavailable (for Django Celery). Listing all the processes by PID for the port then killing them solved connection issues once I restarted Rabbit. My fix is blunt, but underscores where other problems may exist. Celery was giving me "consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: timed out." error locally. – Marc Oct 14 '18 at 19:37
16

you can add these lines to your settings.py :

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'

and run :

celery -A yourProjectName worker -l info
Amir.S
  • 719
  • 8
  • 15
  • Make sure you have redis installed. Please install it from here https://github.com/rgl/redis/downloads Then, go to programfiles/redis/ and open command prompt and write `redis-server` next open redis client – Sumit Kumar Gupta Jun 01 '21 at 08:44
4

If you are workling on a production environment,

You have to first install and setup a rabbitmq server. You can refer rabbitmq website for installation steps.

In settings you have to write this lines:

CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
BROKER_URL = 'amqp://guest:guest@localhost:5672//'

After all setup of rabitmq server you have to run this two command,

export C_FORCE_ROOT='true'
celery -A transcoder(name of app) worker --loglevel=info
jatinkumar patel
  • 2,920
  • 21
  • 28
0

This error means celery backend settings is not proper so it is not able to connect to backend.

If you just started tutorial celery with django . You can try below settings.

CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
CELERY_RESULT_BACKEND = 'db+sqlite:///results.db'
0

While you are installing Celery via pip you should select the version that you want to work with such as redis, rabbitmq, django etc.

As referred in docs: https://pypi.org/project/celery/

Bundles Celery also defines a group of bundles that can be used to install Celery and the dependencies for a given feature.

You can specify these in your requirements or on the pip command-line by using brackets. Multiple bundles can be specified by separating them by commas.

Example:

$ pip install "celery[librabbitmq]"

$ pip install "celery[librabbitmq,redis,auth,msgpack]"
furkanayd
  • 811
  • 7
  • 19
-1

If all above solutions are not working, you can try with this: turn off your network connection (wifi or wire).

It's so weird but sometime it's work for me!

Seem it relates to the local network device instead of RabbitMQ service.

My screen recording: https://drive.google.com/file/d/13t35Lzh3JLsCbGjRag0uiDhcYz1JuHq7/view?usp=sharing

CK.Nguyen
  • 1,258
  • 17
  • 16
-1

Make sure you run the rabbit server in global, in my case that was the issue.