You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku环境下Celery与Redis连接失败问题求助

Heroku环境下Celery与Redis连接失败问题求助

每次调用使用Celery的API端点时,我都会碰到这个错误:

ERROR:celery.backends.redis:Connection to Redis lost: Retry (0/20) now.

我真的搞不懂哪里出问题了——本地环境里一切运行正常,但部署到Heroku就不行。我已经配置好REDIS_URL环境变量了,而且在本地测试连接AWS的Redis主机也是成功的,可就是找不到问题根源。

另外,我把代码推送到Heroku时,日志里明明显示已经连接到Redis了:

Connected to rediss://:**@ec2-34-194-16-21.compute-1.amazonaws.com:11930//

但一调用API端点,就会弹出前面那个连接丢失的错误。

下面是我的相关代码片段:

celery_app.py

from celery import Celery
import ssl

def make_celery(app=None):
    celery = Celery(
        app.import_name if app else "celery_app",
        broker=app.config.get('CELERY_BROKER_URL', 'redis://localhost:6379/0') if app else 'redis://localhost:6379/0',
        backend=app.config.get('result_backend', 'redis://localhost:6379/0') if app else 'redis://localhost:6379/0'
    )
    
    if app:
        celery.conf.update(app.config)

    # Configure SSL for broker and backend if using rediss://
    if celery.conf.broker_url.startswith('rediss://'):
        celery.conf.update(
            broker_use_ssl={
                'ssl_cert_reqs': ssl.CERT_NONE # Enforce SSL validation
            }
        )

    if celery.conf.result_backend.startswith('rediss://'):
        celery.conf.update(
            redis_backend_use_ssl={
                'ssl_cert_reqs': ssl.CERT_NONE # Enforce SSL validation
            }
        )

    return celery

app.py

app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
app.config['result_backend'] = os.getenv('REDIS_URL', 'redis://localhost:6379/0')

# Initialize Celery with Flask app
celery = make_celery(app)

备注:内容来源于stack exchange,提问作者davidocodes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 14:23:05