skip to Main Content
(venv) [riyad@Fury django_celery]$ celery -A django_celery worker -l info
<Celery django_celery at 0x7f0917d57fa0>
 
 -------------- celery@Fury v4.3.0 (rhubarb)
---- **** ----- 
--- * ***  * -- Linux-5.8.6-1-MANJARO-x86_64-with-glibc2.2.5 2020-09-30 02:54:36
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         django_celery:0x7f0917d57fa0
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . django_celery.celery.debug_task
  . example.task.sleepy

[2020-09-30 02:54:36,989: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Trying again in 2.00 seconds...

[2020-09-30 02:54:38,996: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Trying again in 4.00 seconds..

the main issue cause by this redis broker url, so far i am running a redis server on my other terminal. i am using two libraries redis and celery
my settings.py file->

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient"
        },
        "KEY_PREFIX": "example"
    }
}
BROKER_TRANSPORT = "redis"

BROKER_HOST = "localhost"  # Maps to redis host.
BROKER_PORT = 6379         # Maps to redis port.
BROKER_VHOST = "1"         # Maps to database number.

CELERY_RESULT_BACKEND = "redis"
CELERY_REDIS_HOST = "localhost"
CELERY_REDIS_PORT = 6379
CELERY_REDIS_DB = 1

The default configuration from celery docs….all i am doing here is to make a celery instance an to work with the redis broker which can queue the task in redis broker and later fetch task from that my celery.py file->

import os

from celery import Celery

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

app = Celery('django_celery')
print(app)
# Using a string here means the worker doesn'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(f'Request: {self.request!r}')
.

initializing the celery instance with django in main app my init.py file->

from __future__ import absolute_import, unicode_literals

from .celery import app as celery_app

__all__ = ('celery_app',)

2

Answers


  1. set CELERY_BROKER_URL in your django settings

    CELERY_BROKER_URL = "redis://127.0.0.1:6379/1"
    
    Login or Signup to reply.
  2. Instead of all those settings, simply use:

    CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
    

    Alternatively, you can pass the url into your celery app’s constructor:

    app = Celery('django_celery', broker_url='redis://127.0.0.1:6379/0')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search