Psutil Module Introduction
Psutil is an open-source, cross-platform library that provides convenient functions for obtaining information about the system, such as CPU, memory, disk, network, etc. In addition, Psutil can also be used for process management, including determining whether a process exists, getting a list of processes, obtaining process details, and so on. Psutil also offers a number of features provided by command-line tools, including: Ps,top,lsof,netstat,ifconfig, Who,df,kill,free,
Nice,ionice,iostat,iotop,uptime,pidof,tty,taskset,pmap.
Psutil is a cross-platform library that is found on the official website to support the following operating systems.
-Linux
-Windows
-OSX
-FreeBSD
-OpenBSD
-NetBSD
-Sun Solaris
-AIX
Works with Python versions from 2.6 to 3.X.
Installing the Psutil module
Psutil is a third-party open source project, so it needs to be installed for use. The PIP command is used here to install
PIP3 Install Psutil
Psutil contains exceptions, classes, function functions, and constants, where function functions are used to obtain information about the system, such as CPU, disk, memory, network, and so on. Class is used to implement the management functions of the process.
function function
Depending on the functionality of the function, this is mainly divided into the following categories, the following will be from several dimensions to illustrate the psutil provided by the function function.
CPU Class
Cpu_count (, [logical]): Returns the number of logical CPUs by default, and returns the number of physical CPUs when the parameter set for logical is false.
>>> import psutil>>> psutil.cpu_count () 8>>> psutil.cpu_count (Logical=false) 4
Cpu_percent (, [Percpu],[interval]): Returns the utilization of the CPU, displays the utilization of all physical cores when PERCPU is true, interval is not 0 o'clock, and shows the average utilization during the interval execution time when blocking
>>> import psutil>>> psutil.cpu_percent () 6.7>>> psutil.cpu_percent (percpu=true) [17.7, 0.9, 11.0, 1.0, 11.1, 0.9, 10.7, 0.9]>>> psutil.cpu_percent (percpu=true,interval=2) [16.0, 0.0, 8.5, 1.0, 14.4, 0. 5, 9.5, 1.0]
Cpu_time (, [PERCPU]): Returns the time spent in the CPU as a named tuple (namedtuple), PERCPU represents the time taken to get each CPU
>>> import psutil>>> psutil.cpu_times () scputimes (user=5471.2, nice=0.0, system=5633.92, idle= 1295903.87, iowait=2651.2, irq=16.44, softirq=137.87, steal=0.0, guest=0.0) >>> psutil.cpu_times (percpu=True ) [Scputimes (user=2803.53, nice=0.0, system=2824.3, idle=648996.02, iowait=153.71, irq=16.26, softirq=64.71, steal=0.0 , guest=0.0), Scputimes (user=2667.74, nice=0.0, system=2809.74, idle=646935.11, iowait=2497.58, irq=0.18, softirq= 73.15, steal=0.0, guest=0.0)]
1 >>> cpu_time = psutil.cpu_times ()2 >>> cpu_time.user 3 5471.5745# Use the name of the. Element directly to get the corresponding value
an easy way to get information from named tuples
Cpu_times_percent (, [PERCPU]): Functions and cpu_times are roughly the same, as you can tell by the literal meaning that the function returns a time-consuming scale.
>>> import psutil>>> psutil.cpu_times_percent () scputimes (user=0.3, nice=0.0, system=0.4, idle=99.0 , iowait=0.3, irq=0.0, softirq=0.0, steal=0.0, guest=0.0) >>> psutil.cpu_times_percent (percpu=true) [ Scputimes (user=0.3, nice=0.0, system=0.4, idle=99.3, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0), Scputimes (user=0.3, nice=0.0, system=0.4, idle=98.7, iowait=0.6, irq=0.0, softirq=0.0, steal=0.0, guest=0.0)]
Cpu_stats: Returns the statistics of the CPU in the form of a named tuple, including context switches, interrupts, soft interrupts, and number of system calls.
>>> import psutil>>> psutil.cpu_stats () scpustats (ctx_switches=393538808, interrupts=194683724, soft_interrupts=151546977, Syscalls=0)
Python Module Learning-psutil