Celery 连接到rabbitmq-server 而不是redis-server

问题描述 投票:0回答:6

我有一个 Django 应用程序,我想将其配置为 celery 来运行后台任务。

套餐:

  1. 芹菜==4.2.1

  2. Django==2.1.3

  3. Python==3.5

  4. Redis-服务器==3.0.6

settings.py文件中celery的配置为:

CELERY_BROKER_URL = 'redis://localhost:6379'

CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_BEAT_SCHEDULE = {
    'task-number-one': {
            'task': 'app.tasks.task_number_one',
            'schedule': crontab(minute='*/1'),
    },
}

celery.py 文件:

from __future__ import absolute_import, unicode_literals
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', 'project.settings.prod')

app = Celery('project')

# 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')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

当我跑步时:

celery -A project worker -l info -B -E

它指向 rabmmitmq 服务器,而应该指向 redis-server,如下所示:

 -------------- celery@user-desktop v4.2.1 (windowlicker)
---- **** ----- 
--- * ***  * -- Linux-4.15.0-39-generic-x86_64-with-Ubuntu-18.04-bionic 2018-11-21 12:04:51
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         project:0x7f8b80f78d30
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     redis://localhost:6379/
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: ON
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . app.tasks.task_number_one
  . project.celery.debug_task

[2018-11-21 12:04:51,741: INFO/Beat] beat: Starting...

生产环境也发生同样的情况。 在生产中,我已经使用 Gunicorn 和 Nginx 部署了 Django 应用程序,现在我想实现一些方法来运行后台任务,因为

django-crontab
包不起作用。

问题:

  1. celery配置有什么问题?

  2. 有人可以推荐一种运行定期后台任务的方法吗?

**注意:我尝试过实现supervisor,但似乎supervisor与python3不兼容,因此无法配置它。

django celery background-process redis-server
6个回答
9
投票

v4经纪人 URL 的设置发生了变化。应该是

BROKER_URL
而不是
CELERY_BROKER_URL


8
投票

如果你是从芹菜官网复制

celery.py
的内容
https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

尝试更改以下行,从

app.config_from_object('django.conf:settings', namespace='CELERY')

app.config_from_object('django.conf:settings', namespace='')


4
投票

更换

CELERY_BROKER_URL = 'redis://localhost:6379'
BROKER_URL = 'redis://localhost:6379'
。这对我有用。


1
投票

更改

BROKER_URL
TO
CELERY_BROKER_URL
后必须更改
celery.py

中的这一行
app = Celery('proj')

添加

'backend='redis://localhost', broker='redis://'
,所以它看起来像这样

app = Celery('proj', backend='redis://localhost', broker='redis://')

现在可以工作了:)


0
投票

如果您使用 Redis 作为代理,使用 .delay() 方法进行排队任务,并收到奇怪的连接错误 111 拒绝连接到rabbitmq(您根本不使用),请尝试使用 .apply_async()

这种行为发生在生产中。


0
投票

在我的例子中,问题是使用

CELERY_BROKER_URL
作为环境变量来设置 rediss 端点并使用它创建 Django 设置
CELERY_BROKER_URL

if is_env_var_set("CELERY_BROKER_URL"):
    CELERY_BROKER_URL = (
        "rediss://" +
        os.getenv("CELERY_BROKER_ENDPOINT") +
        ":6379" + "/0" +
        "?ssl_cert_reqs=required"
    )
else:
    CELERY_BROKER_URL = 'redis://localhost:6379/0'

设置环境变量

export CELERY_BROKER_URL="xyz.redis.region.oci.oraclecloud.com"

这导致 celery 使用

amqp
方案而不是定义的
rediss
方案。

CELERY_BROKER_URL
是celery使用的特殊环境变量,使用此变量注入redis端点导致了这个难以调试的问题。

解决方案

if is_env_var_set("CELERY_BROKER_ENDPOINT"):
    CELERY_BROKER_URL = (
        # note the rediss:// instead of redis:// to use SSL
        "rediss://" +
        os.getenv("CELERY_BROKER_ENDPOINT") +
        ":6379" + "/0" +
        "?ssl_cert_reqs=required"
    )
else:
    CELERY_BROKER_URL = 'redis://localhost:6379/0'
© www.soinside.com 2019 - 2024. All rights reserved.