在 Python 中检查子进程的内存使用情况 [英] Check memory usage of subprocess in Python
问题描述
我正在 Ubuntu 上用 Python 开发一个应用程序,我正在使用子进程从 python 中运行外部二进制文件.由于这些二进制文件是在运行时生成的并且可能会出错,因此我需要对这些二进制文件的内存占用量和运行时间保持严格的标签.有什么办法可以在运行时限制或监视这些二进制程序的内存使用情况吗?为此,我真的很讨厌在子进程中使用ps"之类的东西.
I'm developing an application in Python on Ubuntu and I'm running external binaries from within python using subprocess. Since these binaries are generated at run time and can go rogue, I need to keep a strict tab on the amount of memory footprint and runtime of these binaries. Is there someway I can limit or monitor the memory usage of these binary programs at runtime? I would really hate to use something like "ps" in subprocess for this purpose.
推荐答案
有了子进程的 PID 编号,您可以从 proc 文件系统.使用:
Having a PID number of your subprocess you can read all info from proc file-system. Use:
/proc/[PID]/smaps(由于 Linux2.6.14)此文件显示每个进程映射的内存消耗.对于每个映射,都有一个系列行如下:
/proc/[PID]/smaps (since Linux 2.6.14) This file shows memory consumption for each of the process's mappings. For each of mappings there is a series of lines as follows:
或
/proc/[PID]/statm提供有关内存使用情况的信息,以页为单位.
/proc/[PID]/statm Provides information about memory usage, measured in pages.
或者,您可以限制子进程可以获取的资源:
Alternatively you can limit resources which subprocess can aquire with :
subprocess.Popen('ulimit -v 1024; ls', shell=True)
当达到给定的虚拟内存限制时,进程会因内存不足而失败.
When given virtual memory limit is reached process fails with out of memory.
这篇关于在 Python 中检查子进程的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!