Aldarund Aldarund - 1 year ago 103
Python Question

Celery use wrong broker

I have setup from the the docs file:

from __future__ import absolute_import

import os

from celery import Celery

from django.conf import settings

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cars.settings')

app = Celery('cars')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

supervisord config

command = /opt/webapps/bbay/env/bin/celery worker -A cars.celery:app ; Command to start app
directory = /opt/webapps/bbay/
user = bbay ; User to run as
numprocs = 1
stdout_logfile = /opt/webapps/bbay/logs/celery.log ; Where to write log messages
redirect_stderr = true ; Save stderr in the same log
autostart = true
autorestart = true
startsecs = 10

django settings :

BROKER_URL = 'redis://localhost:6379/0'

when i start worker all seems fine and correct broker url used:

(env)bbay@djproj:/opt/webapps/bbay$ celery -A cars.celery:app beat
celery beat v3.1.17 (Cipater) is starting.
__ - ... __ - _
Configuration ->
. broker -> redis://localhost:6379/0
. loader ->
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%INFO
. maxinterval -> now (0s)

Here is my task:

def send_mail_task(template, context, send_to):

Here is how i use it:

send_mail_task.delay('email/confirmation_message.html', context, [, ])

But when the task called it tried to connect to default broker ( host

'' ). Here is stacktrace:

Stacktrace (most recent call last):

File "django/core/handlers/", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "django/views/decorators/", line 57, in wrapped_view
return view_func(*args, **kwargs)
File "django/views/generic/", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "rest_framework/", line 452, in dispatch
response = self.handle_exception(exc)
File "rest_framework/", line 449, in dispatch
response = handler(request, *args, **kwargs)
File "accounts/api/", line 132, in post
send_mail_task.delay('email/contact_seller.html', context, [, ])
File "celery/app/", line 453, in delay
return self.apply_async(args, kwargs)
File "celery/app/", line 555, in apply_async
**dict(self._get_exec_options(), **options)
File "celery/app/", line 355, in send_task
reply_to=reply_to or self.oid, **options
File "celery/app/", line 305, in publish_task
File "kombu/", line 168, in publish
routing_key, mandatory, immediate, exchange, declare)
File "kombu/", line 457, in _ensured
File "kombu/", line 369, in ensure_connection
interval_start, interval_step, interval_max, callback)
File "kombu/utils/", line 243, in retry_over_time
return fun(*args, **kwargs)
File "kombu/", line 237, in connect
return self.connection
File "kombu/", line 741, in connection
self._connection = self._establish_connection()
File "kombu/", line 696, in _establish_connection
conn = self.transport.establish_connection()
File "kombu/transport/", line 112, in establish_connection
conn = self.Connection(**opts)
File "amqp/", line 165, in __init__
self.transport = self.Transport(host, connect_timeout, ssl)
File "amqp/", line 186, in Transport
return create_transport(host, connect_timeout, ssl)
File "amqp/", line 299, in create_transport
return TCPTransport(host, connect_timeout)
File "amqp/", line 95, in __init__
raise socket.error(last_err)

So what is wrong and how to make celery connect to the specified broker and where in celery docs is it?

Answer Source

The problem was that i missed celery in should contain following ( from the docs ):

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app