简体   繁体   English

从已完成的 celery 任务中清除 redis

[英]Clearing out redis from celery tasks that has finished

I'm using celery task queue which use Redis as a message broker to run some background tasks that save results to Redis.我正在使用 celery 任务队列,它使用 Redis 作为消息代理来运行一些后台任务,将结果保存到 Redis。

I wanted to clear out Redis from task results that did finish because i no longer need them and so in case of having a big spike I shouldn't worry.我想从完成的任务结果中清除 Redis,因为我不再需要它们,所以如果出现大峰值,我不应该担心。

I came across CELERY_RESULT_EXPIRES which indicates that a task result will automatically clean up after X seconds.我遇到了CELERY_RESULT_EXPIRES ,它表示任务结果将在 X 秒后自动清理。

Consider the following code:考虑以下代码:

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()

When running the code above and having celery beat -A tasks -l info running too, then checking Redis after a while (more than 5mins) I see this:当运行上面的代码并让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"

Tasks 4 to 6 which were completed still there, Isn't the results supposed to clear out after 15 seconds?完成的任务4到6还在那里,不是应该15秒后清除结果吗? Am I missing something here?我在这里错过了什么吗?

Thanks in advance.提前致谢。

@zhong So the thing you are missing is celery.backend_cleanup as stated in docs @zhong 所以你缺少的是 celery.backend_cleanup 如文档中所述

"A built-in periodic task will delete the results after this time", so built in tasks run by default at 4 am daily, so if you don't change the configuration it will be deleted for sure, you can cross check that after 4am, in case if you are planning to store that in db you have to run celery beat for that, “内置定时任务会在这个时间后删除结果”,所以内置任务默认每天凌晨4点运行,所以如果你不改变配置肯定会被删除,你可以交叉检查之后凌晨 4 点,如果您打算将其存储在数据库中,则必须为此运行 celery beat,

So to recap it will be deleted yes, but only when celery backend cleaner will run, the one which you are configuring determines lifetime of that result that need to be stored, but that doesn't mean redis will delete that for you, that expiry team is for celery cleaner to look and determine that, yes this result now can be deleted.所以回顾一下,它会被删除,是的,但只有当 celery 后端清理器将运行时,您正在配置的那个决定了需要存储的结果的生命周期,但这并不意味着 redis 将为您删除它,即到期团队正在让 celery 清洁器查看并确定,是的,现在可以删除此结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM