Celery 连接到 amqp://guest**@localhost 而不是 Redis。Heroku 部署

tik*_*kej 1 python django heroku celery

我已经阅读了类似的主题并完成了建议的所有内容,但问题仍然存在。

我正在将我的应用程序部署到 Heroku。本地一切工作正常,但在指定每个设置后的部署过程中,我可以考虑指定 celery 工作人员发送以下错误:

:22:50.722826 + 00:00应用程序[worker.1]:[2018-10-21 18:22:50,722:错误/MainProcess]消费者:无法连接到amqp://guest:**@127.0.0.1:5672 //: [Errno 111] 连接被拒绝。

我尝试从 CloudAMQP 切换到 Redis。问题仍然存在。

这是我的配置文件:

Django的settings.py:

try: 
    CELERY_BROKER_URL = os.environ['REDIS_URL']
except KeyError: 
    CELERY_BROKER_URL = 'amqp://admin:admin@localhost:5672/admin_host'

try:
    CELERY_RESULT_BACKEND = os.environ['REDIS_URL']
except KeyError:
    CELERY_RESULT_BACKEND = 'amqp://admin:admin@localhost:5672/admin_host'


CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

django_heroku.settings(locals())
Run Code Online (Sandbox Code Playgroud)

芹菜.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from . import settings

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

app = Celery('file_upload', broker_pool_limit=1)

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

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

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

__init__.py 来自包含 celery.py 和 settings.py 的包:

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']
Run Code Online (Sandbox Code Playgroud)

并且os.environ['REDIS_URL']确实返回了一个网址,我检查了它。

有人帮忙吗?

tik*_*kej 5

与这件事斗争了几天,在发布此内容后我自己回答了。有人可能会发现这很有用。

就我而言,解决这个问题的关键是我celery.py在创建 celery 应用程序对象时硬编码了 redis URL 并将其作为参数传递:

app = Celery('file_upload', broker_pool_limit=1, broker=redis_url,
             result_backend=redis_url)
Run Code Online (Sandbox Code Playgroud)