Python 3.6 Installation of psutil modules and functional introduction

  • 2020-10-23 20:10:38
  • OfStack

1. psutil module

1. psutil is a cross-platform library, which can easily obtain the process and system utilization (including CPU, memory, disk, network, etc.) information of the running system. It is mainly used for system monitoring, analyzing and limiting the management of system resources and processes. It implements functions provided by equivalent command line tools such as ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iotop, uptime, pidof, tty, taskset, pmap, etc. It currently supports 32-bit and 64-bit Linux, Windows, OS X, FreeBSD and Sun Solaris operating systems.

psutil download link (website) : https: / / pypi python. org/pypi psutil /

psutil download address (github) : https: / / github com/giampaolo/psutil /

2. window10 operating system (Python 3.6 development environment) installation of psutil


D:\Program Files\python\Scripts>pip.exe install D:\python\psutil-5.2.2-cp36-cp36m-win_amd64.whl 
Processing d:\python\psutil-5.2.2-cp36-cp36m-win_amd64.whl 
Installing collected packages: psutil 
Successfully installed psutil-5.2.2 

psutil. whl file to be uploaded:

Use 3.

Get system performance information (CPU, memory, disk, network)

3.1 CPU related

View cpu information


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)

Displays all logical information for cpu


>>> psutil.cpu_times(percpu=True)
[scputimes(user=11684.17, nice=57.93, system=148683.01, idle=2168982.08, iowait=260833.18, irq=7882.35, softirq=0.0, steal=3697.3, guest=0.0, guest_nice=0.0)]

View the user's cpu time ratio


>>> psutil.cpu_times().user
11684.4

View the logical number of cpu


>>> psutil.cpu_count()
1

View the number of cpu physics


>>> psutil.cpu_count(logical=False)
1

3.2 View system memory


>>> import psutil
>>> mem = psutil.virtual_memory()
>>> mem
# All information about system memory 
svmem(total=1040662528, available=175054848, percent=83.2, used=965718016, free=74944512, active=566755328, inactive=59457536, buffers=9342976, cached=90767360)

Total system memory


>>> mem.total
1040662528

The system is already using memory


>>> mem.used
965718016

System free memory


>>> mem.free
112779264

Gets swap memory information


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
0

Read disk parameters

Disk utilization is obtained using psutil.es111EN_ES112en method,

Disk IO information includes read_count(read IO number), write_count(write IO number)
read_bytes(IO writing stander), read_time(disk read time), write_time(disk write time), these IO messages are used


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
1

Get complete information about the disk


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
2

Gets the parameters of the partition table


psutil.disk_usage('/')  # To obtain / State of partition 

Gets the total number of hard drives IO


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
4

Gets the number of IO for a single partition


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
5

Read network information

The network information is similar to the disk IO information and involves several key points, including byes_sent(number of bytes sent),byte_recv=xxx(number of bytes received),
pack-ets_sent =xxx(number of bytes sent), ES161en-ets_ES163en =xxx(number of packets received), which are used for network information

Obtain total IO information of the network


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
6

Output information for each interface of the network


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
7

Gets the current system user login information


psutil.users()

Get boot time

psutil.boot_time () # is returned in linux time format

datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S") # converted to natural time format

System process management

Get the current system process information, get the current program running state, including the start time of the process, view the Settings CPU affinity, memory usage,IO information
socket connection, number of threads, etc

Get process information

View all system processes


import Psutil
# To view cpu All the information 
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)
9

View a single process


p = psutil.Process(2423) 
p.name()  # The process of 
p.exe()  # The process of bin The path 
p.cwd()  # The absolute path to the working directory of the process 
p.status()  # Process status 
p.create_time() # Process creation time 
p.uids()  # process uid information 
p.gids()  # The process of gid information 
p.cpu_times()  # The process of cpu Time information , including user,system two cpu information 
p.cpu_affinity() #get process cpu affinity , If You want to set cpu affinity , will cpu Just for reference 
p.memory_percent() # Process memory utilization 
p.memory_info()  # Process memory rss,vms information 
p.io_counters()  # The process of IO information , Including reading and writing IO Numbers and Parameters 
p.connectios()  # Return process list 
p.num_threads() # Number of threads open by the process 
 heard psutil the Popen Method to start the application and track information about the application 
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"],stdout=PIPE)
p.name()
p.username()

Related articles: