Hello! Hello, I'm kimiko! On time to update you!
Ps: little story 🌲
Two days ago, a fan friend asked me in the group whether we can still check it without installing some well-known computer management software
CPU usage rate, or can introduce a relatively small plug-in awesome. Indeed, many people don't like it in life
Install those management software.
Today, mumuzi will introduce you a small script of handwritten programming code, which can be easily displayed on the interface: using Python to implement
Monitor CPU usage when.
It can be displayed at any time without downloading the management software. Dozens of lines of code can be done~
The complete material + source code of all articles are in 👇👇
Module introduction 🌴
🌳 1.0 detailed usage reference of module
psutil file: https://psutil.readthedocs.io/en/latest/ matplotlib file: https://matplotlib.org/users/index.html
🌳 1.1 using psutil module
Psutil is a cross platform library( http://code.google.com/p/psutil/ ), it can easily obtain the process and information of system operation
System utilization (including CPU, memory, disk, network, etc.) information. It is mainly used in system monitoring, analyzing and limiting system resources
Source and process management.
It implements the functions provided by the same command-line tools, such as ps, top, lsof, netstat, ifconfig, who, df, kill, free
nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap, etc.
At present, it supports 32-bit and 64 bit operating systems such as Linux, Windows, OS X, FreeBSD and Sun Solaris.
🌳 1.2 use matplotlib module
Matplotlib is a Python 2D graphics library, which generates publishing quality in a variety of hard copy formats and cross platform interactive environments
Rank graphical . Through Matplotlib, developers can generate graphs, histograms and power spectra with just a few lines of code,
Bar chart, error chart, scatter chart, etc.
2, In preparation 🌴
🌳 2.1 relevant environment
Python3.6,Pycharm. Related modules: matplotlib module; psutil module.
🌳 2.2 installation
Here, the small series are used uniformly: pip install -i https://pypi.douban.com/simple / + module name
3, Start typing code 🌴
🌳 3.1 code content
Monitor CPU usage in real time with Python:
Execute user process
Execute kernel processes and interrupts
CPU is idle
It is mainly the coding process of these three aspects.
🌳 3.2 complete code attached
import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager import psutil as p POINTS = 300 fig, ax = plt.subplots() ax.set_ylim([0, 100]) ax.set_xlim([0, POINTS]) ax.set_autoscale_on(False) ax.set_xticks() ax.set_yticks(range(0, 101, 10)) ax.grid(True) # Percentage of time spent executing user processes user = [None] * POINT # Percentage of time spent executing kernel processes and interrupts sys = [None] * POINT # Percentage of time the CPU is idle idle = [None] * POINT l_user, = ax.plot(range(POINTS), user, label='User %') l_sys, = ax.plot(range(POINTS), sys, label='Sys %') l_idle, = ax.plot(range(POINTS), idle, label='Idle %') ax.legend(loc='upper center', ncol=4, prop=font_manager.FontProperties(size=10)) bg = fig.canvas.copy_from_bbox(ax.bbox) def cpu_usage(): t = p.cpu_times() return [t.user, t.system, t.idle] before = cpu_usage() def get_cpu_usage(): global before now = cpu_usage() delta = [now[i] - before[i] for i in range(len(now))] total = sum(delta) before = now return [(100.0*dt)/(total+0.1) for dt in delta] def OnTimer(ax): global user, sys, idle, bg tmp = get_cpu_usage() user = user[1:] + [tmp] sys = sys[1:] + [tmp] idle = idle[1:] + [tmp] l_user.set_ydata(user) l_sys.set_ydata(sys) l_idle.set_ydata(idle) while True: try: ax.draw_artist(l_user) ax.draw_artist(l_sys) ax.draw_artist(l_idle) break except: pass ax.figure.canvas.draw() def start_monitor(): timer = fig.canvas.new_timer(interval=100) timer.add_callback(OnTimer, ax) timer.start() plt.show() if __name__ == '__main__': start_monitor()
4, Effect display 🌴
🌳 4.1 dynamic video display——
Use Python to monitor CPU utilization in real time!
🌳 4.2 static screenshot display——
Hee hee, the article ends here. After fans get the code, they feel that this gadget is still very practical and has a little partner of love
You can ask me for the source code and eat it at ease~
🎯 Complete free source code collection: find me! You can get it yourself at the end of the article, and so can didi!
🎉 Some previous articles recommended——
Item 1.3 video player
Item 3.2 automatic wallpaper change
Item 3.3 # WordArt signature
Item 3.9 # code to live in the snowy scenery and snow flying applet
Project 4.0 # GIF making magic (Douluo continent as an example)
🎄 Article summary——
(more content + source code are summarized in the article!! welcome to read ~)