网络编程
位置:首页>> 网络编程>> Python编程>> python实现可视化动态CPU性能监控

python实现可视化动态CPU性能监控

作者:Paspi  发布时间:2023-08-08 10:23:52 

标签:python,CPU,性能监控

本文实例为大家分享了python可视化动态CPU性能监控的具体代码,供大家参考,具体内容如下

打算开发web性能监控,以后会去学js,现在用matp来补救下,在官网有此类模板,花了一点时间修改了下,有兴趣的可以去官网看看。

基于matplotoilb和psutil,matplotoilb是有名的数据数据可视化工具,psutil是性能监控工具,所以你需要这两个环境,本文不多说环境的安装。

以下是代码:


#!/usr/bin/env python
#-*-coding:utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import psutil
def data_gen(t=0): #设置xy变量
x = 0  
y = 1
while True:
y = psutil.cpu_percent(interval=1) #获取cpu数值,1s获取一次。
x += 1
yield x,y    
def init():
ax.set_xlim(0, 10)   #起始x 1-10
ax.set_ylim(0, 100)   #设置y相当于0%-100%
del xdata[:]
del ydata[:]
line.set_data(xdata, ydata)
return line,

fig, ax = plt.subplots()
line, = ax.plot([], [], lw=2)  #线像素比
ax.grid()
xdata, ydata = [], []

def run(data):
# update the data
t, y = data
xdata.append(t)
ydata.append(y)
xmin, xmax = ax.get_xlim()

if t >= xmax:   #表格随数据移动
ax.set_xlim(xmin+10, xmax+10)
ax.figure.canvas.draw()
line.set_data(xdata, ydata)

return line,

ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10,
repeat=False, init_func=init)
plt.show()

下面是效果图,还有很多地方不完善,以后会花点时间完成。

python实现可视化动态CPU性能监控

来源:https://blog.csdn.net/qq_33083319/article/details/53672278

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com