【问题标题】:How to make django celery use the redis broker url?如何让 django celery 使用 redis 代理 url?
【发布时间】:2021-01-15 16:46:02
【问题描述】:
(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..

这个 redis 代理 url 引起的主要问题,到目前为止,我正在我的另一个终端上运行一个 redis 服务器。我正在使用两个库 redis 和 celery 我的 settings.py 文件->

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

来自 celery docs 的默认配置....我在这里所做的只是让一个 celery 实例与 redis 代理一起工作,它可以在 redis 代理中排队任务,然后从我的 celery.py 文件中获取任务->

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}')
.

在主应用程序我的 init.py 文件中使用 django 初始化 celery 实例->

from __future__ import absolute_import, unicode_literals

from .celery import app as celery_app

__all__ = ('celery_app',)

【问题讨论】:

标签: django redis celery


【解决方案1】:

不用所有这些设置,只需使用:

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

或者,您可以将 url 传递给 celery 应用的构造函数:

app = Celery('django_celery', broker_url='redis://127.0.0.1:6379/0')

【讨论】:

    【解决方案2】:

    在您的 django 设置中设置 CELERY_BROKER_URL

    CELERY_BROKER_URL = "redis://127.0.0.1:6379/1"
    

    【讨论】:

      猜你喜欢
      • 2020-06-29
      • 2013-06-15
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-04
      • 2014-07-19
      • 1970-01-01
      相关资源
      最近更新 更多