【问题标题】:Celery is connecting to amqp://guest**@localhost instead of Redis. Heroku deployCelery 连接到 amqp://guest**@localhost 而不是 Redis。 Heroku 部署
【发布时间】:2019-03-25 21:23:28
【问题描述】:

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

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

:22:50.722826+00:00 app[worker.1]: [2018-10-21 18:22:50,722: ERROR/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())

celery.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))

__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']

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

有人帮忙吗?

【问题讨论】:

    标签: python django heroku celery


    【解决方案1】:

    几天与这个东西打架,就在发布这个之后,我回答了自己。有人可能会觉得这很有用。

    在我的情况下,解决这个问题的关键是我在 celery.py 中硬编码了 redis URL,并在创建 celery app 对象时将其作为参数传递:

    app = Celery('file_upload', broker_pool_limit=1, broker=redis_url,
                 result_backend=redis_url)
    

    【讨论】:

    • 感谢您在 Stackoverflow 上留下这个问题和答案!我已经为此工作了好几天。很有用!
    【解决方案2】:

    为了解决这个问题,我认为需要做的是将 CELERY_BROKER_URL var 更改为 BROKER_URL。如果在从对象中获取配置时给它命名空间 CELERY,它应该只是 CELERY_BROKER_URL。 更改自:

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

    收件人:

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

    【讨论】:

      【解决方案3】:

      在本地/开发环境中,当我们使用kombu而不是ampq时,celery broker url配置如下:

      CELERY_BROKER_URL = 'django://'  # for example
      

      在您的 celery.py 中,对构造函数的调用以创建 celery 应用程序必须如下设置:

      django_url = settings.CELERY_BROKER_URL
      app = Celery('tone_insurance', broker=django_url, result_backend=django_url)
      

      这是基于答案here

      【讨论】:

        猜你喜欢
        • 2020-06-07
        • 2020-12-30
        • 2013-08-10
        • 2019-01-10
        • 2017-07-12
        • 2012-05-26
        • 2022-11-10
        • 1970-01-01
        • 2015-12-09
        相关资源
        最近更新 更多