Python程序监控CPU利用率并写入文件
#__author__ = 'chubby_superman' #_*_coding=utf-8 _*_ import psutil import time import os import matplotlib.dates import matplotlib.pyplot as plt def cpu_count(): print('-----------------------------cpu信息---------------------------------------' + '\n') print(u"物理CPU个数: %s" % psutil.cpu_count(logical=False)) print(u"CPU核心总数: %s" % psutil.cpu_count()) f = open('cpu.txt', 'a') f.write('-----------------------------cpu信息---------------------------------------') f.write(u"物理CPU个数: %s" % psutil.cpu_count(logical=False) + '\n') f.write(u"CPU核心总数: %s" % psutil.cpu_count() + '\n') cpu1=psutil.cpu_percent(1) cpu = (str(cpu1))+ '%' sys=time.localtime(time.time()) sys_time = time.strftime("%H:%M:%S",sys) print(u"cup使用率: %s" % cpu) f.write(u"cup使用率: %s" % cpu + '\n') f.close() cpu_list=["cup使用率",cpu1,sys_time] return cpu_list def a(): while 1: yield cpu_count() if __name__=="__main__": fig=plt.figure(figsize=(10,5)) plt.xlabel("time") plt.xticks(rotation=60) plt.ylim(0,100) plt.yticks([a for a in range(101) if a%5==0]) plt.ylabel("data") plt.title("test") plt.grid(True) #添加网格 plt.ion() #interactive mode on try: while 1: data = next(a()) plt.plot(data[2],data[1],linewidth = '1', label = "cpu",marker = 'o') plt.pause(0.1) except Exception as e: plt.ioff() plt.show()