【发布时间】:2020-11-14 19:11:38
【问题描述】:
我正在使用 celery 任务队列,它使用 Redis 作为消息代理来运行一些将结果保存到 Redis 的后台任务。
我想从确实完成的任务结果中清除 Redis,因为我不再需要它们,所以如果出现大幅峰值,我不应该担心。
我遇到了CELERY_RESULT_EXPIRES,它表示任务结果将在 X 秒后自动清理。
考虑以下代码:
from celery import Celery
CONFIG = {
'BROKER_URL': 'redis://localhost:6379/0',
'CELERY_RESULT_BACKEND': 'redis://localhost:6379/0',
'CELERY_RESULT_EXPIRES': 15, # 15 secs
'BROKER_POOL_LIMIT': 0, # redis connection get closed once task is done..
}
celery = Celery('tasks', config_source=CONFIG)
@celery.task(name='tasks.say_hello')
def say_hello():
return "Helloooooo i just came out from the background!"
if __name__ == '__main__':
say_hello.delay().get()
当运行上面的代码并同时运行celery beat -A tasks -l info,然后在一段时间后(超过 5 分钟)检查 Redis,我看到了:
127.0.0.1:6379> KEYS *
1) "_kombu.binding.celery"
2) "_kombu.binding.celery.pidbox"
3) "_kombu.binding.celeryev"
4) "celery-task-meta-854543d8-14ad-4bf8-9725-edcf64131bb2"
5) "celery-task-meta-fa3e267e-46d0-4488-a766-d3276b6abdeb"
6) "celery-task-meta-86c2d83c-cadd-41b9-b4ff-426607786299"
已完成的任务 4 到 6 还在那里!结果不是应该在 15 秒后清除吗,我在这里遗漏了什么吗?
提前致谢。
【问题讨论】:
-
你用的是哪个版本的芹菜?
-
@2ps 4.4.6(悬崖)
标签: python django redis celery