Celery无法连接到redis服务器。

3

我有一个使用celery==4.2.1,redis==2.10.6和redis-server=4.0.9的Django 2.0.5应用程序。当我启动celery worker时,会得到以下输出:

-------------- celery@octopus v4.2.1 (windowlicker)
---- **** ----- 
--- * ***  * -- Linux-4.18.16-surface-linux-surface-x86_64-with-Ubuntu-18.04-bionic 2018-10-31 17:33:50
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         MemorabiliaJSON:0x7fd6c537b240
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

但是在我的Django设置中,我有以下内容:
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_IMPORTS = ('memorabilia.tasks',
                  'face_recognition.tasks',
                  )

我的 celery.py 文件如下:

# http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.apps import apps
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MemorabiliaJSON.settings.tsunami')

app = Celery('MemorabiliaJSON')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

相同的代码(通过我的git服务器共享)在我的开发机上运行,尽管redis服务器有点旧 - v=2.8.4。开发机是Ubunutu 14.04,笔记本电脑是Ubuntu 18.04。我所说的“运作正常”是指这是我开发机上的celery输出:

 -------------- celery@tsunami v4.2.1 (windowlicker)
---- **** ----- 
--- * ***  * -- Linux-4.4.0-138-generic-x86_64-with-Ubuntu-14.04-trusty 2018-10-31 17:38:09
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         MemorabiliaJSON:0x7f356e024c18
- ** ---------- .> transport:   redis://localhost:6379//
- ** ---------- .> results:     redis://localhost:6379/
- *** --- * --- .> concurrency: 8 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

我该如何让celery读取除了celery.py之外的django配置文件呢?
谢谢!
Mark

忘记提到redis-server正在运行,我可以调用redis-cli ping并获得PONG响应。 - user1045680
7个回答

16

localhost更改为127.0.0.1在我的情况下解决了问题:

CELERY_BROKER_URL = 'redis://127.0.0.1:6379'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_STORE_ERRORS_EVEN_IF_IGNORED = True

1
这对我有用,我在这个问题上浪费了一天的时间。 - Vikas Gautam
如果你学到了什么或者知道为什么它“确切地”不起作用,那么你就没有浪费时间。 - Shiplu Mokaddim

2

我正在使用celery和redis。曾经遇到同样的问题,我像这样解决了它

在你的celery.py文件中,找到以下代码行

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

你只需要删除namespace="CELERY",最终你的代码应该是这样的:
     app.config_from_object("django.conf:settings")

这在我的情况下完美地工作。 首先是这样的

然后,在更新上述设置之后


1

我曾经遇到过同样的问题。通过使用正确的设置文件来运行celery,解决了这个问题。你的配置是完全正确的,问题在于你运行celery的位置。你可能提供了错误的应用程序模块,或者在运行celery时根本没有提供它。

you need to specify --app=<module_which_contains_your_celery_file>

具体语法如下:

celery --app=<APPLICATION> worker

当没有指定--app或-A参数时,Celery会使用传输URL = amqp://guest:@localhost:5672//**。

这也可以从您的Celery日志中观察到:

在第一个Celery日志中:

- ** ---------- .> transport:  amqp://guest:**@localhost:5672//
- ** ---------- .> results:     disabled://

在另一个(工作中的)Celery日志中:

- ** ---------- .> transport:   redis://localhost:6379//
- ** ---------- .> results:     redis://localhost:6379/

1

如果您仔细查看来自celery@octopus的芹菜输出,您会发现它连接到一个amqp代理而不是redis代理:amqp://guest:**@localhost:5672//。这意味着您的章鱼工作程序已经在某个地方配置为指向rabbitmq代理,而不是redis代理。为了纠正这一点,您需要找到rabbitmq代理配置设置的位置,并查看如何将其引入芹菜中。因为broker_url告诉我们的是,芹菜正在其他地方被重新配置,或者服务器上应用了其他设置。


我看到celery试图连接rabbitmq,但是rabbitmq没有安装。而且如上所示,celery已经配置为使用redis。 - user1045680

0

完全不相关于你是如何解决的,但我曾经遇到过类似的问题,即配置文件中的“transport”URL正在寻找端口5672(而不是Redis的6379,结果URL是正确的)。在早期调试时,我已从app.config_from_object中删除了命名空间。将其放回去解决了我的问题。在这里提供给任何犯同样错误并来到这里的人。


0
为了结束这个问题,我将回答它。 老实说,我不确定我如何解决这个问题,但是在做了一些更改和重新启动我的系统后,它就消失了。设置与上面相同。
后来我发现我有一个命名模块的问题,即两个模块具有相同的名称。 一旦我纠正了这个问题,我的大多数其他芹菜问题就解决了。 但是,要明确的是,在解决模块命名问题之前,redis / celery 部分是有效的。
感谢所有给我的问题提出建议的人!

0
请按照以下方式更新CELERY_BROKER_URL
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

您可以在这里查看有关将Redis作为代理连接的文档。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接