程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of the use of psutil module in python (classic programming case of python3)

編輯:Python

One . psutil Module introduction

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

Two . psutil Use of modules

2.1 obtain cpu Information

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

2.2 Get memory information

import psutil
# 1. Get the usage of system memory 
print(psutil.virtual_memory())
# 2. Get statistics of system swap memory 
print(psutil.swap_memory())

2.3 Get disk information

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

2.4 Get network information

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

2.5 Get other system information

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

2.6 Analog out ps Effect of command

print(psutil.test())

2.7 With json Return to the process in the form of pid And name

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 !!!️


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved