Celery未连接到Redis服务器

时间:2018-11-01 00:41:31

标签: django redis celery

我有一个使用celery == 4.2.1,redis == 2.10.6,redis-server = 4.0.9的django 2.0.5应用程序。当我启动芹菜工作者时,我得到以下输出:

-------------- 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。开发机器是Ubunut 14.04,笔记本电脑是Ubuntu 18.04。通过工作,我的意思是这是我的开发机器上的芹菜输出:

 -------------- 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.py中的内容外,我如何让celery读取django配置文件?

谢谢!

标记

6 个答案:

答案 0 :(得分:6)

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 :(得分:0)

请按照以下方式更新CELERY_BROKER_URL

CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

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

答案 2 :(得分:0)

如果您仔细查看celery@octopus的芹菜输出,您会发现它与amqp代理而不是Redis代理连接:amqp://guest:**@localhost:5672//。这意味着您的章鱼工作人员已配置为指向Rabbitmq代理而不是Redis代理。为了纠正这个问题,您必须找到Rabbitmq代理配置的位置,并查看如何将其拉入芹菜。因为broker_url告诉我们的是celery正在以其他方式重新配置,或者服务器上正在应用其他设置。

答案 3 :(得分:0)

为了结束这个问题,我将回答它。老实说,我不确定如何解决此问题,但是在进行了一些更改并重新启动系统后,问题就消失了。设置仍与上面相同。

后来,我发现模块命名存在问题,因为两个模块的名称相同。一旦纠正了这个问题,其他大多数芹菜问题就消失了。但是,需要明确的是,在修复模块命名问题之前,redis / celery部分已经正常工作。

感谢所有向我的问题发表建议的人!

答案 4 :(得分:0)

与您如何解决它完全无关,但是我遇到了一个类似的问题,其中配置中的“ transport” URL正在寻找端口5672(而不是Redis的6379,结果URL是正确的)。在进行早期调试时,我从app.config_from_object中删除了名称空间。放回去解决了我的问题。把它放在这里给那些犯同样错误并且来到这里的人

答案 5 :(得分:0)

我正在使用celery&redis。遇到了同样的问题,我解决了

在您的celery.py中转到行

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

您只需要删除命名空间=“ CELERY”,最后您的代码应为

     app.config_from_object("django.conf:settings")

就我而言,这非常完美。 firstly it was like this

还有After update above settings