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.
In this article, we will take an in-depth look at how to use the PSUtil module to monitor the various resources of your system. This article includes the following topics:
1. Install the psutil module
First, we need to install the psutil module. It can be installed using the following command:
pip install psutil
2. Obtaining system information
Use the psutil.cpu_count() function to get the CPU count:
import psutil print("Number of CPUs:", psutil.cpu_count(logical=True))
As you can see, we used the psutil.cpu_count() function to get the number of logical CPUs in the system.
Next, we can use the psutil.cpu_stats() function to get CPU statistics:
import psutil cpu_stats = psutil.cpu_stats() print("CPU statistics:", cpu_stats)
3. Get CPU information
Use the psutil.cpu_percent() function to get the CPU utilization:
import psutil print("CPU utilization:", psutil.cpu_percent(interval=1))
As you can see, we used the psutil.cpu_percent() function to get the CPU utilization. interval parameter specifies the time interval in seconds.
Next, we can use the psutil.cpu_times() function to get CPU time information:
import psutil cpu_times = psutil.cpu_times() print("CPU time information:", cpu_times)
4. Getting memory information
Use the psutil.virtual_memory() function to get memory information:
import psutil memory = psutil.virtual_memory() print("Memory information:", memory)
5. Getting Disk Information
Use the psutil.disk_partitions() function to get disk partition information:
import psutil partitions = psutil.disk_partitions() print("Disk Partition Information:", partitions)
As you can see, we used the psutil.disk_partitions() function to get information about the disk partitions in the system.
Next, we can use the psutil.disk_usage() function to get disk usage:
import psutil usage = psutil.disk_usage('/') print("Disk Usage:", usage)
6. Access to network information
Use the psutil.net_io_counters() function to get network information:
import psutil net_io_counters = psutil.net_io_counters() print("Network Information:", net_io_counters)
As you can see, we used the psutil.net_io_counters() function to get information about the network, including the number of bytes sent and received.
7. Getting sensor information
Use the psutil.sensors_temperatures() function to get sensor information:
import psutil sensors_temperatures = psutil.sensors_temperatures() print("Sensor information:", sensors_temperatures)
As you can see, we used the psutil.sensors_temperatures() function to get information about the sensors in the system.
8. Process management using psutil
Use the psutil.process_iter() function to get the running process:
import psutil for process in psutil.process_iter(): print(())
As you can see, we used the psutil.process_iter() function to get the running process.
We can use various properties of the process object to get detailed information about the process.
Use the () function to get details about a specific process:
import psutil process_id = 1234 process = (process_id) print(()) print(process.cpu_percent(interval=1)) print(process.memory_info().rss)
9. Using psutil for system monitoring
Implement a simple system monitoring program using psutil:
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 utilization:", cpu_usage) print("Memory utilization:", memory_usage) print("Disk utilization:", disk_usage) print("Web usage:", network_usage) (1)
As you can see, we used psutil to implement a simple system monitor program that can monitor CPU, memory, disk and network usage in real time.
10. Summary
This article describes how to use psutil module to monitor various resources of the system. We can use psutil to get system information, CPU information, memory information, disk information, network information and sensor information.
We can also use psutil for process management and system monitoring. psutil is a very powerful Python library that can help us write efficient system monitoring programs.
To this article on the use of Python PsUtil to achieve real-time monitoring of the system status of the article is introduced to this, more related Python PsUtil monitor the system status of the content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!