Celery + Redis 失去连接

Celery + Redis losing connection

我有一个非常简单的 Celery 任务,运行是一个(long 运行ning)shell 脚本:

import os
from celery import Celery

os.environ['CELERY_TIMEZONE'] = 'Europe/Rome'
os.environ['TIMEZONE'] = 'Europe/Rome'

app = Celery('tasks', backend='redis', broker='redis://OTHER_SERVER:6379/0')

@app.task(name='ct.execute_script')
def execute_script(command):
    return os.system(command)

我在 运行 服务器 MY_SERVER 上有这个任务,我从 OTHER_SERVER 启动它,那里也是 运行 宁 Redis 数据库。 任务似乎 运行 成功(我看到在文件系统上执行脚本的结果)但我总是开始收到以下错误:

INTERNAL ERROR: ConnectionError('Error 111 connecting to localhost:6379. Connection refused.',)

会是什么?为什么在我将 Redis 服务器设置为 redis://OTHER_SERVER:6379/0 并且它有效(自任务启动后)时它尝试联系 localhost?谢谢

当您设置 backend 参数时,Celery 将使用它作为结果后端。
在您的代码中,您告诉 Celery 使用本地 redis 服务器作为结果后端。

你看到了ConnectionError,因为celery无法将结果保存到本地redis服务器
您可以禁用结果后端或启动本地 redis 服务器或将其设置为 OTHER_SERVER.

参考: