Home > Article > Backend Development > How to use PsUtil in Python to monitor system status in real time
PSUtil is a cross-platform Python library for retrieving information about running processes and system utilization (CPU, memory, disk, network, sensors).
It is cross-platform and runs on Linux, Windows and macOS.
First, we need to install the psutil module. You can use the following command to install:
pip install psutil
Use the psutil.cpu_count() function to get the number of CPUs:
import psutil print("CPU数量:", psutil.cpu_count(logical=True))
As you can see, we use psutil The .cpu_count() function obtains the number of logical CPUs in the system.
Next, we can use the psutil.cpu_stats() function to obtain CPU statistical information:
import psutil cpu_stats = psutil.cpu_stats() print("CPU统计信息:", cpu_stats)
Use the psutil.cpu_percent() function to obtain CPU usage:
import psutil print("CPU使用率:", psutil.cpu_percent(interval=1))
As you can see, we use the psutil.cpu_percent() function to obtain the CPU usage. The interval parameter specifies the time interval in seconds.
Next, we can use the psutil.cpu_times() function to obtain the CPU time information:
import psutil cpu_times = psutil.cpu_times() print("CPU时间信息:", cpu_times)
Use the psutil.virtual_memory() function to obtain Memory information:
import psutil memory = psutil.virtual_memory() print("内存信息:", memory)
Use psutil.disk_partitions() function to get disk partition information:
import psutil partitions = psutil.disk_partitions() print("磁盘分区信息:", partitions)
As you can see, we use psutil.disk_partitions( ) function obtains the disk partition information in the system.
Next, we can use the psutil.disk_usage() function to get the disk usage:
import psutil usage = psutil.disk_usage('/') print("磁盘使用情况:", usage)
Use the psutil.net_io_counters() function to get the network Information:
import psutil net_io_counters = psutil.net_io_counters() print("网络信息:", net_io_counters)
As you can see, we used the psutil.net_io_counters() function to obtain network information, including the number of bytes sent and received.
Use the psutil.sensors_temperatures() function to obtain sensor information:
import psutil sensors_temperatures = psutil.sensors_temperatures() print("传感器信息:", sensors_temperatures)
As you can see, we use the psutil.sensors_temperatures() function to obtain the system sensor information.
Use the psutil.process_iter() function to obtain the running process:
import psutil for process in psutil.process_iter(): print(process.name())
As you can see, we use psutil.process_iter() The function gets the running process.
We can use various properties of the process object to obtain detailed information about the process.
Use psutil.Process() function to obtain detailed information of a specific process:
import psutil process_id = 1234 process = psutil.Process(process_id) print(process.name()) print(process.cpu_percent(interval=1)) print(process.memory_info().rss)
Use psutil to implement a simple system monitoring program:
import time import psutil while True: cpu_usage = psutil.cpu_percent(interval=1) memory_usage = psutil.virtual_memory().percent disk_usage = psutil.disk_usage('/').percent network_usage = psutil.net_io_counters().bytes_sent + psutil.net_io_counters().bytes_recv print("CPU使用率:", cpu_usage) print("内存使用率:", memory_usage) print("磁盘使用率:", disk_usage) print("网络使用量:", network_usage) time.sleep(1)
As you can see, we use psutil to implement a simple system monitoring program that can monitor CPU, memory, disk and network usage in real time.
The above is the detailed content of How to use PsUtil in Python to monitor system status in real time. For more information, please follow other related articles on the PHP Chinese website!