Python程序监控CPU利用率并写入文件

匿名 (未验证) 提交于 2019-12-02 22:51:30

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()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!