Celery任务执行成功但Redis中无任务结果键的问题求助
Celery任务执行成功但Redis中无任务结果键的问题求助
大家好,我现在碰到一个挺奇怪的问题:我用Redis同时作为Celery的消息中间件(broker)和结果后端(result backend),任务能正常被接收、执行,返回的结果也完全正确,但就是在Redis里找不到任何和任务相关的键。我用Redis Commander可视化工具查过,也进到容器里用Redis CLI检查了所有数据库,结果都是空的,想请教下这到底是怎么回事?
我的配置和代码
tasks.py
from celery import Celery from time import sleep app = Celery("tasks", broker="redis://localhost:6379", backend="redis://localhost:6379") app.conf.result_expires = 3600 # 1小时结果留存 @app.task def process(x, y): for _ in range(5): sleep(10) print("Processing") return x**2 + y**2
docker-compose.yml
services: redis: image: redis ports: - "6379:6379" redis-commander: image: rediscommander/redis-commander ports: - "8081:8081" environment: - REDIS_HOSTS=local:redis:6379
运行步骤和日志
- 启动Celery Worker的命令
celery -A tasks worker --loglevel=info -E
Worker启动日志:
-------------- celery@Parvizs-MacBook-Air.local v5.5.3 (immunity) --- ***** ----- -- ******* ---- macOS-14.6.1-arm64-arm-64bit-Mach-O 2025-07-23 - *** --- * --- - ** ---------- [config] - ** ---------- .> app: tasks:0x10305c830 - ** ---------- .> transport: redis://localhost:6379// - ** ---------- .> results: redis://localhost:6379/ - *** --- * --- .> concurrency: 8 (prefork) -- ******* ---- .> task events: ON --- ***** ----- -------------- [queues] .> celery exchange=celery(direct) key=celery
- 执行任务
在Python Shell里运行:
process.delay(12, 12)
Celery任务执行日志:
Task tasks.process[89899d0a-cb18-4ed7-8d9c-e8a442e9eb21] received Processing Processing Processing Processing Processing Task tasks.process[89899d0a-cb18-4ed7-8d9c-e8a442e9eb21] succeeded in 50.07s: 288
- 获取结果
能正常从后端拿到结果:
result = process.AsyncResult("89899d0a-cb18-4ed7-8d9c-e8a442e9eb21") result.get() # 返回288
我的排查操作
我进到Redis容器里检查键:
docker exec -it <container_id> redis-cli 127.0.0.1:6379> SELECT 0 127.0.0.1:6379> KEYS * (empty list)
我把Redis的0到15号数据库都检查了一遍,结果全是空的。
按道理Redis应该是正常工作的(毕竟任务能跑通、结果能获取),但为什么就是看不到Celery相关的键呢?有没有人遇到过类似的问题?
内容来源于stack exchange
相关产品推荐
相关产品推荐

