The advanced characteristics and skills of the PStuil class library in python

Advanced features and skills of PSUTIL library in python Overview: PSUTIL is a cross -platform Python library that provides detailed information about computer resource utilization by obtaining system information and process information.This article will introduce some of the advanced characteristics and skills of the PSUTIL library to help you better use the library. 1. Install the PSUTIL library: To use the PSUTIL library, it must be installed first.You can use the PIP command to install, as shown below: python pip install psutil After the installation is completed, we can import the PSUTIL module and use the functions and classes in it. 2. Get CPU information: Using PSUTIL's `cpu_count ()` function can obtain the CPU core number in the system, as shown below: python import psutil cpu_count = psutil.cpu_count() Print ("CPU Core Number:", CPU_COUNT) The output result is similar to: CPU core number: 8 3. Get memory information: Using psutil's `virtual_memory ()` function can obtain the detailed information of the system memory, as shown below: python import psutil memory_info = psutil.virtual_memory() Total = Memory_info.total // (1024 ** 3) # Convert bytes to GB available = memory_info.available // (1024 ** 3) percent = memory_info.percent Print ("Total memory:", Total, "GB") Print ("Available memory:", available, "gb") Print ("memory usage:", percent, "%") The output result is similar to: Total memory: 16 GB Available memory: 8 GB Memory usage rate: 50.0 % 4. Get disk information: Using psutil's `DISK_USAGE ()` function can obtain the use of the disk, as shown below: python import psutil disk_usage = psutil.disk_usage('/') TOTAL = DISK_USAGE.TOTAL // (1024 ** 3) # Convert bytes to GB used = disk_usage.used // (1024 ** 3) free = disk_usage.free // (1024 ** 3) percent = disk_usage.percent Print ("Total Space of Disk:", Total, "GB") Print ("Use space:", use, "gb") Print ("Available space:", free, "gb") Print ("Disk use rate:", percent, "%") The output result is similar to: Total space of disk: 237 GB Use space: 142 GB Available space: 95 gb Disk usage: 60.0 % 5. Get process information: Using PSUTIL's `Process_iter ()` function and `AS_DICT ()` method can obtain the detailed information of all processes running in the current system, as shown below: python import psutil for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']): process_info = proc.as_dict(attrs=['pid', 'name', 'cpu_percent']) Print ("Process ID:", Process_info ['pid']) Print ("Process Name:", Process_info ['name']) Print ("CPU usage rate:", proces_info ['cpu_percent'], "%") print("-------------------------------") The output result is similar to: Process ID: 4321 Process name: python CPU usage rate: 10.0 % ------------------------------- Process ID: 5678 Process name: chrome CPU usage rate: 5.0 % ------------------------------- ... You can modify the parameters of the `AS_DICT ()` method to obtain other process information. 6. Other common functions: -We using the `net_connections ()` function to obtain information from the network connection. -Ad the system startup time with `Boot_time ()` function. -Ad the information of the current login user with the `users ()` function. -Ad the function of using the `pid_exists (pid)` function to check whether the process of the specified process ID exists. in conclusion: This article introduces some of the advanced characteristics and techniques of the PSUTIL library, including detailed information about obtaining the CPU, memory, disk and process.You can use the PSUTIL library to monitor the use of system resources according to your needs and perform corresponding processing and optimization.