简体   繁体   English

Celery/Redis 任务过期

[英]Celery/Redis task expiration

I am using Celery with supervisor running the workers and Redis as the broker, and I'm having an issue with a Celery worker apparently freezing up, making it unable to process any more tasks and causing its task queue in Redis to fill up to the point of causing some memory issues.我正在使用 Celery,主管运行工人,Redis 作为代理,我遇到了一个 Celery 工人明显冻结的问题,使其无法处理更多任务并导致其在 Redis 中的任务队列填满导致一些内存问题的点。 I tried setting the expires option when I called the task, thinking that this would take advantage of Redis' support for key expiry:我在调用任务时尝试设置expires选项,认为这将利用 Redis 对密钥过期的支持:

some_task.apply_async(args=('foo',), expires=60)

but this didn't work, and when I inspected the corresponding list in the Redis CLI, it just kept expanding – perhaps unsurprisingly, because it sounds like list expiry is not built-in functionality in Redis.但这没有用,当我检查 Redis CLI 中的相应列表时,它一直在扩展——也许并不奇怪,因为听起来列表过期不是 Redis 的内置功能 The Celery docs say that the expiration time corresponds to time after "publishing" the task, but I couldn't find any mention of what "publishing" actually means. Celery 文档说过期时间对应于“发布”任务后的时间,但我找不到任何关于“发布”实际含义的提及。 I had assumed it referred to adding the task to the Redis list, so either that presumption is wrong or something else is going on that I don't understand (or both).我假设它指的是将任务添加到 Redis 列表中,所以要么这个假设是错误的,要么发生了我不明白的事情(或两者)。

Am I wrong about task expiry time?我对任务到期时间有误吗? And if so, is there any way to cause the messages to expire within Redis?如果是这样,是否有任何方法可以使消息在 Redis 中过期?

The context is more puzzling than the question.上下文比问题更令人费解。 You are able to use redis-cli and you inspected the redis keys.您可以使用 redis-cli 并检查了 redis 密钥。 In redis-cli, you can type ttl sexykey and you should have seen the remaining seconds or not if the key was set to expire when written there by Celery and thus answer that particular uncertainty on your part regarding this matter.在 redis-cli 中,您可以键入ttl sexykey ,如果 Celery 将密钥设置为过期,您应该已经看到剩余的秒数,从而回答您对此事的特定不确定性。

First, let us be clear there is a message broker .首先,让我们清楚有一个message broker Second, there is a result backend .其次,有一个result backend Celery only has a very few message brokers but many result backends . Celery 只有很少的message brokers ,但有很多result backends The list of brokers is here .The list of supported backends is at page 10 ( as of 2018-Mar-24 ) under Transport and Backends section here .经纪人列表在这里。支持的后端列表位于第 10 页(截至 2018 年 3 月 24 日传输和后端部分下。 It is the result backend that I assume would fill-up because this is what I am seeing too.这是我认为会填满的result backend ,因为这也是我所看到的。

Celery can use the same Redis instance both as a message broker and a result backend . Celery 可以使用同一个 Redis 实例作为message brokerresult backend Celery stores as a Redis key the result of an executed task irregardless of wither the task succeeds or not and this redis key has a default expiration of 1 day (86400 seconds). Celery 将执行任务的结果存储为 Redis 键,而不管任务是否成功,并且此 Redis 键的默认有效期为 1 天(86400 秒)。 So if you have many function calls executed by Celery, then your Redis in-memory cache would fill-up because the key expiration of 86400 seconds will not catch-up with the incoming recording of tasks results.因此,如果您有许多由 Celery 执行的函数调用,那么您的 Redis 内存缓存将被填满,因为 86400 秒的密钥过期时间将赶不上任务结果的传入记录。

To shorten the key expiration to 60 seconds, here is the python snippet:要将密钥过期时间缩短到 60 秒,这里是 python 片段:

app = Celery('justdoit',
    broker='redis://172.17.0.2',
    backend='redis://172.17.0.2')

app.conf.result_expires = 60

PS: I was just learning Celery a few hours ago and I immediately recognized ( before it happens ) this very same Redis filling-up scenario as described. PS:几个小时前我刚刚学习 Celery,我立即意识到(在它发生之前)与所描述的 Redis 填满场景完全相同。 I have been using Redis for a year so I know some of its characteristics.我已经使用 Redis 一年了,所以我知道它的一些特性。

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

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