Detailed explanation of using psutil library

1. Background

In the world of Python, there are some libraries that are loved by developers for their powerful functions and ease of use. Today, we are going to introduce one of them - the psutil library. psutil (python system and process utilities) is a cross-platform third-party library used to obtain process and system utilization (including CPU, memory, disk, network, etc.) information when the system is running. It is mainly used for system monitoring, performance analysis, process management and other scenarios.

2. Installation & Basic Use

psutil installation:

pip install psutil

After the installation is complete, we can start using the psutil library. Below, we will introduce some commonly used functions.

1. Get CPU information

The psutil library can obtain CPU usage. For example, we can use psutil.cpu_percent(interval=1) to get the CPU usage.

import psutil
cpu_percent = psutil.cpu_percent(interval=1)print(f'CPU usage: {cpu_percent}%')

2. Obtain memory information

We can use psutil.virtual_memory() to get the memory usage of the system.

import psutil
mem_info = psutil.virtual_memory()print(f'Total memory: {mem_info.total / (1024**3):.2f} GB')print(f'Used memory: {mem_info.used / (1024**3):.2f} GB')print(f'Memory usage: {mem_info.percent}%')

3. Get disk information

The psutil library can also obtain disk usage. For example, we can use psutil.disk_usage('/') to get the disk usage of the root directory.

import psutil
disk_usage = psutil.disk_usage('/')print(f'Total disk space: {disk_usage.total / (1024**3):.2f} GB')print(f'Used disk space: {disk_usage.used / (1024**3):.2f} GB')print(f'Disk usage: {disk_usage.percent}%')

4. Obtain process information

The psutil library can also obtain information about all processes running in the system. For example, we can use psutil.pids() to get the PIDs of all processes.

import psutil
pids = psutil.pids()print(f'Total processes: {len(pids)}')

3. Practical project application

picture

Suppose there is such a requirement: run the Pycharm program for a long time and monitor the CPU/memory usage of the Pycharm program to verify whether the CPU usage increases or memory leaks occur when the Pycharm program is opened for a long time.

Based on such needs, we can use the psutil library and the pandas library to complete it. The script is as follows:

1. Obtain the overall CPU and memory usage of the computer

# 获取电脑整体的CPU、内存占用情况def getMemory():    data = psutil.virtual_memory()    memory = str(int(round(data.percent))) + "%"    print("系统整体memory占用:"+memory)    return memory

def getCpu():    cpu_list=psutil.cpu_percent(percpu=True)    average_cpu = round(sum(cpu_list) / len(cpu_list),2)    cpu=str(average_cpu) + "%"    print("系统整体cpu占用:"+cpu)    return cpu

2. Obtain the CPU and memory usage information code of the specified process

# 获取指定进程的CPU和内存占用信息代码def getMemSize(pid):    # 根据进程号来获取进程的内存大小    process = psutil.Process(pid)    memInfo = process.memory_info()
    # rss: 该进程实际使用物理内存(包含共享库占用的全部内存)。    # vms:该进程使用的虚拟内存总量。
    return memInfo.rss / 1024 / 1024
def getCpuPercent(pid):    # 根据进程号来获取进程的内存大小    p = psutil.Process(pid)    p_cpu = p.cpu_percent(interval=0.1)    cpu = round(p_cpu,2)    return cpu
def getTotalM(processName):    # 一个进程名对应的可能有多个进程    # 进程号才是进程的唯一标识符,进程名不是    totalM = 0    for i in psutil.process_iter():        if i.name() == processName:            totalM += getMemSize(i.pid)    print('进程占用内存:%.2f MB' % totalM)    finalM=round(totalM,2)    return finalM
def getTotalCPU(processName):    # 一个进程名对应的可能有多个进程    # 进程号才是进程的唯一标识符,进程名不是    totalCPU = 0    for i in psutil.process_iter():        if i.name() == processName:            totalCPU += getCpuPercent(i.pid)    totalCPU_convert=round(totalCPU,2)    finalCPU=str(totalCPU_convert)+'%'    print("进程占用CPU:"+finalCPU)    return totalCPU_convert

3. Write test result data to csv file

# 将测试结果数据写入csv文件def writeExcel(caseName,cpu,mem,pycharmcpu,pycharmmem):    timestamp = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime(time.time()))    dict = {'caseName': [caseName], 'Sys_CPU': [cpu], 'Sys_Memory': [mem], 'Pycharm_Cpu': [pycharmcpu], 'Pycharm_Mem': [pycharmmem],'OperationTime':[timestamp]}
    # 字典中的key值即为csv中列名    dataframe = pd.DataFrame(dict)    dataframe['OperationTime'] = pd.to_datetime(dataframe['OperationTime'])
    # 将DataFrame存储为csv, mode='a'表示每一次都是追加内容而不是覆盖,header=False表示不写列名    dataframe.to_csv("cpuAndMemtest.csv", date_format='%Y-%m-%d-%H:%M:%S', mode='a',index=False,header=False,encoding='GBK')

4. Encapsulate the method as a function so that it can be called directly later

# 封装方法为函数,以便后续直接调用def getCpuAndMem(caseName,processName1):    memory = getMemory()    cpu = getCpu()    # 获取pycharm64.exe进程占用的CPU和内存    pycharmmem = getTotalM(processName1)    pycharmcpu = str(getTotalCPU(processName1))+'%'
    time.sleep(1)    writeExcel(caseName,cpu,memory,pycharmcpu,pycharmmem)    print("系统整体CPU占用:%s     系统整体内存占用:%s   进程_CPU占用:%s  进程内存占用:%s"%(cpu, memory, pycharmcpu, pycharmmem))    print("===============================================================")

5. Run the script

Guess you like

Origin blog.csdn.net/davice_li/article/details/131730056