psutil(process and system utilities)
It's a cross platform library , Used in Python Retrieve information about running processes and system utilization (CPU、 Memory 、 disk 、 The Internet 、 sensor ) Information about .
It is mainly used for system monitoring 、 Analyze and limit process resources and manage running processes .
It implements the classic UNIX Many functions provided by command line tools , Such as ps、top、iotop、lsof、netstat、ifconfig、free etc. .
psutil Modules can be used across platforms , Support Linux/UNIX/OSX/Windows etc. , It is mainly used for system monitoring , Performance analysis , Process management, etc .
stay python in , have access to psutil This third-party module is used to obtain information .
github Address :https://github.com/giampaolo/psutil
Official documents :https://psutil.readthedocs.io/en/latest/
install :pip3 install psutil
import psutil
# 1. obtain CPU The complete information of
print(psutil.cpu_times())
# 2. obtain CPU The number of logics
print(psutil.cpu_count())
# 3. obtain CPU The number of physics of
print(psutil.cpu_count(logical=False))
# 4. psutil Get the system CPU The method of utilization rate is cpu_percent(), It has two parameters , Namely interval and percpu
# interval It specifies the calculation cpu Time interval of utilization ,percpu Specify whether to select the total utilization rate or each cpu The usage rate of
for x in range(10):
print(psutil.cpu_percent(interval=1))
print(psutil.cpu_percent(interval=1,percpu=True))
import psutil
# 1. Get the usage of system memory
print(psutil.virtual_memory())
# 2. Get statistics of system swap memory
print(psutil.swap_memory())
# 1. Get information about disk partition
print(psutil.disk_partitions())
# 2. Get disk usage
print(psutil.disk_usage('/'))
# 3. Get the IO Statistics ( Reading and writing speed, etc )
print(psutil.disk_io_counters())
# 1. Get the total network IO Information
print(psutil.net_io_counters())
# 2. Get the network card's IO Information
print(psutil.net_io_counters(pernic=True))
# 3. Get network interface information
print(psutil.net_if_addrs())
# 4. Get network interface status information
print(psutil.net_if_stats())
# Get the startup time of the system , And transform it into a natural format
print(psutil.boot_time())
# Get the list of users connected to the system
print(psutil.users())
# Get all the process information of the system
print(psutil.pids())
# Get information about a single process , Get the specified process ID=780
print(psutil.Process(780))
print(psutil.test())
for proc in psutil.process_iter(['pid', 'name']):
print(proc.info)
# Run results into the next :
{
'pid': 35907, 'name': 'Google Chrome Helper (Renderer)'}
{
'pid': 36575, 'name': 'com.apple.Safari.SafeBrowsing.Service'}
{
'pid': 36729, 'name': 'com.apple.AppleU'}
️ If it works , You can pay attention to or collect it !!!️