使用python实现根据进程名获取该进程一段时间的内存占用,并画出曲线图

上一篇 / 下一篇  2017-04-24 14:52:40 / 个人分类:技术分享


pidManageGUI.py
import tkinter as tk
import sys
import pidManage as pid
sys.path.append(r"C:\Users\dll\PycharmProjects\pypro")



def btnHelloClicked():
try:
cd = int(entryCd2.get())
pidName = str(entryCd1.get())
if cd <= 0 or cd >5000:
labelHello.config(text="请输入0--500之间的整数!")
else:
labelHello.config(text="正在画图,请等待...")
try:
pid.plotMemoryVsTime(pidName, cd)
except:
labelHello.config(text="输入有误!")
except ValueError:
labelHello.config(text="未找到所需进程!!!")


top = tk.Tk()
top.title("绘制进程内存图")
top.geometry("250x150")
labelHello = tk.Label(top, text="提示信息", height=5, width=20, fg="blue")
label1 = tk.Label(top, text="进程名:").grid(row=0, sticky='W')
label2 = tk.Label(top, text="采样数:").grid(row=2, sticky='W')
entryCd1 = tk.Entry(top, text="")
entryCd1.grid(row=0, column=1, sticky="E")
entryCd2 = tk.Entry(top, text="请输入采样点数")
entryCd2.grid(row=2, column=1, sticky='E')
btnCal = tk.Button(top, text="画图", command=btnHelloClicked)
btnCal.grid(row=3, column=1, sticky='E')
labelHello.grid(row=4, column=1, sticky="S")
top.mainloop()

pidManage.py
# coding = utf-8
import psutil as pt
import matplotlib.pyplot as plt
import time


def getPidByName(Str):
pids = pt.process_iter()
pidList = []
for pid in pids:
if pid.name() == Str:
pidList.append(pid.pid)
#print(pidList)
return pidList[3]


def getMemByPid(pid, sampleNum):
PID = pt.Process(pid)
memoryList = []
for i in range(0, sampleNum):
memory = PID.memory_info()
memoryList.append(memory.rss/1024/1024)
time.sleep(2)
#print(memoryList)
return memoryList


def returnTime():
startTime = time.time()
startTime = int(startTime)
return startTime

def plotMemoryVsTime(pidName, sampleNum):
PID = getPidByName(pidName)
plt.figure(1)
T = returnTime()
#print("正在获取样本,请等待。。。")
memoryList = getMemByPid(PID, sampleNum)
#print("正在绘图,请等待。。。")
plt.plot([t for t in range(0, len(memoryList))], [m for m in memoryList])
#print("绘图完成!!!")
plt.show()
time.sleep(5)

TAG: Python python 曲线图

 

评分:0

我来说两句

我的栏目

日历

« 2024-04-16  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 7776
  • 日志数: 3
  • 建立时间: 2017-04-17
  • 更新时间: 2017-04-27

RSS订阅

Open Toolbar