简体   繁体   English

如何分析 celery 任务的 memory 使用情况?

[英]how to profile memory usage of a celery task?

I have a django application that runs background tasks using the celery lib and I need to obtain and store the max memory usage of a task.我有一个 django 应用程序,它使用 celery 库运行后台任务,我需要获取并存储任务的最大 memory 使用量。

I've tried memory_usage from memory_profiler library, but I can not use this function inside a task because I get the error: "daemonic processes not allowed have children".我已经尝试了 memory_profiler 库中的 memory_usage,但我无法在任务中使用这个 function,因为我收到错误:“不允许有子进程的守护进程”。 I've also tried the memory_usage function outside the task, to monitor the task.async call, but for some reason the task is triggered twice.我还尝试了任务外的 memory_usage function 来监视 task.async 调用,但由于某种原因,该任务被触发了两次。

All the other ways I found out there consist of checking the memory usage in different places of the code and then getting the maximum, but I have the feeling that it is very inaccurate and there are probably some calls that have a high memory usage that is left out because of garbage collection before I manage to check the current memory usage.我在那里发现的所有其他方法包括检查代码不同位置的 memory 使用情况,然后获得最大值,但我觉得它非常不准确,可能有些调用的 memory 使用率很高在我设法检查当前的 memory 使用情况之前,因为垃圾收集而被遗漏了。

the official documentation has some useful functions but it would have to rely on the method above.官方文档有一些有用的功能,但它必须依赖于上面的方法。 https://docs.celeryproject.org/en/latest/reference/celery.utils.debug.html https://docs.celeryproject.org/en/latest/reference/celery.utils.debug.html

Thanks in advance!提前致谢!

Why not a controller tasks?为什么不是controller任务?

Celery infrastructure let to query the current status of all workers: Celery 基础设施让我们查询所有工作人员的当前状态:

from celery import Celery
app = Celery(...)
app.control.inspect().active()

This can be used inside a task to poll every # sec the cluster and understand what's happening.这可以在任务中使用,每 # 秒轮询一次集群并了解正在发生的事情。 I've used a similar approach to identify and send the kill() command between tasks.我使用了类似的方法在任务之间识别和发送kill()命令。 My tasks are killable so each of them know how to handle the soft kill.我的任务是可杀的,所以他们每个人都知道如何处理软杀。

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

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