心 空

  • Tools
Ricky Hao
  1. 首页
  2. Python
  3. 正文

Celery 任务生产者在大规模生产任务时,存在的内存累积问题

29 7 月, 2019 3917点热度 0人点赞 0条评论

简述

在当前的项目中,由于项目需求,需要实现一个将一个大任务拆分成无数个子任务的功能。
而在这个功能实现的时候,由于拆分的子任务数量太大,导致内存占用一直攀升直至Out of Memory。
为了解决这个问题,我曾经怀疑过由RabbitMQ的HeartBeat带来的内存泄露问题,但是在经过了tracemalloc内存分析之后,发觉这个问题的原因其实是Celery默认开启的一个小功能(坑)。

产生原因

在Celery文档中,有一个参数Task.apply_async.add_to_parent,这个参数的主要作用是:
* add_to_parent (bool) – If set to True (default) and the task is applied while executing another task, then the result will be appended to the parent tasks request.children attribute. Trailing can also be disabled by default using the trail attribute

即,默认情况下,一个主任务在运行时产生的子任务会自动被添加到主任务的children列表里,并且会跟踪其运行状态。
但是在当前的需求中,我们不需要对子任务的状态进行跟踪,所以只要将add_to_parent这个参数改为False即可。

task.apply_async(add_to_parent=False)
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: celery python
最后更新:29 7 月, 2019

Ricky

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。

COPYRIGHT © 2025 心 空. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang