Skip to main content
 首页 » 编程设计

python之如何获取外部程序的内存使用情况之python

2025年05月04日12birdshome

我试图在我的 python 脚本中获取外部程序的内存使用情况。我试过使用脚本 http://code.activestate.com/recipes/286222/如下:

m0 = memory() 
subprocess.call('My program') 
m1 = memory(m0) 
print m1 

但这似乎只是给我 python 脚本的内存使用情况,而不是“我的程序”。有没有办法输出程序的内存使用情况,以便在 python 脚本中使用?

请您参考如下方法:

尝试使用 Psutil

import psutil 
import subprocess 
import time 
 
SLICE_IN_SECONDS = 1 
p = subprocess.Popen('calling/your/program') 
resultTable = [] 
while p.poll() == None: 
  resultTable.append(psutil.get_memory_info(p.pid)) 
  time.sleep(SLICE_IN_SECONDS)