Python之psutil-进程管理
在给PC端应用做自动化测试或者监测应用性能时,不可避免的会与进程管理打交道,python中的psutil模块能够帮助我们处理进程,它主要用于系统监视、分析和限制进程资源以及管理正在运行的进程。一起来了解psutil的用法吧。
import psutil # 获取所有正在运行的进程PID pid_list = psutil.pids() print(pid_list) # 获取指定PID的进程 p = psutil.Process(19712) print(p) print("进程名:", p.name()) print("进程的可执行绝对路径:", p.exe()) print("进程当前的工作目录:", p.cwd()) print("调用此进程的命令行:", p.cmdline()) print("进程PID:", p.pid) print("进程的父PID:", p.ppid()) print("进程的子进程:", p.children()) print("进程的父进程(对象):", p.parent()) print("进程的父进程(列表):", p.parents()) print("进程的状态:", p.status()) print("拥有该进程的用户:", p.username()) print("进程创建的时间:", p.create_time()) print("进程累计的时间:", p.cpu_times()) print("进程的CPU占用率:", p.cpu_percent()) print("进程的CPU亲和性:", p.cpu_affinity()) print("进程的内存信息:", p.memory_info()) print("进程的内存占用率:", p.memory_percent()) print("进程映射的内存区域:", p.memory_maps()) print("进程的I/O信息:", p.io_counters()) print("进程打开的文件信息:", p.open_files()) print("进程打开的套接字连接:", p.connections()) print("进程使用的线程数:", p.num_threads()) print("进程打开的线程:", p.threads()) print("进程执行的上下文切换数量:", p.num_ctx_switches()) print("进程的优先级(带参数则可设置优先级):", p.nice()) print("进程的I/O优先级(带参数则可设置优先级):", p.ionice()) print("进程的环境变量:", p.environ()) print("进程信息(字典):", p.as_dict()) print("进程是否正在运行:", p.is_running())
运行结果:
[0, 4, 120, 128, 476, 492, 548, 616, 632, 756, 776, 872, 884, 940, 944, 952, 1068, 1124, 1132, 1204, 1264, 1328, 1336, 1344, 1436, 1444, 1480, 1524, 1552, 1596, 1612, 1656, 1764, 1776, 1784, 1788, 1804, 1836, 1860, 1916, 1924, 1932, 1940, 1948, 1992, 2036, 2044, 2100, 2108, 2172, 2188, 2256, 2380, 2400, 2588, 2680, 2748, 2816, 2828, 2900, 2944, 2968, 3060, 3068, 3088, 3156, 3272, 3328, 3348, 3456, 3468, 3704, 3720, 3776, 3784, 3896, 3904, 3920, 3928, 3952, 3960, 3988, 4004, 4048, 4056, 4068, 4088, 4100, 4124, 4160, 4172, 4372, 4428, 4444, 4472, 4492, 4504, 4520, 4536, 4588, 4600, 4616, 4628, 4640, 4764, 4888, 4896, 4928, 4944, 4952, 5052, 5068, 5140, 5192, 5228, 5288, 5588, 5616, 5652, 5808, 5832, 5856, 5980, 6288, 6364, 6388, 6436, 6456, 6556, 6716, 6776, 7144, 7452, 7508, 7516, 8072, 8248, 8284, 8372, 8408, 8440, 8604, 8612, 8620, 8628, 8632, 8788, 8832, 8852, 9028, 9096, 9116, 9136, 9376, 9572, 9692, 9700, 9788, 10384, 10460, 10480, 11052, 11096, 11272, 11320, 11544, 11656, 11992, 12012, 12136, 12196, 12244, 12556, 12640, 12788, 12860, 12892, 12948, 12984, 13028, 13036, 13192, 13280, 13476, 13644, 13692, 14228, 14464, 14576, 14640, 15088, 15248, 15560, 15644, 15708, 15720, 16072, 16184, 16496, 16616, 16848, 16924, 17004, 17084, 17208, 17484, 17688, 17848, 17852, 17888, 17920, 18184, 18384, 18476, 18616, 18664, 18676, 18868, 19036, 19044, 19052, 19648, 19712, 19880, 19916, 20524, 21220] psutil.Process(pid=19712, name='chrome.exe', status='running', started='09:52:53') 进程名: chrome.exe 进程的可执行绝对路径: C:\Program Files\Google\Chrome\Application\chrome.exe 进程当前的工作目录: C:\Program Files\Google\Chrome\Application\92.0.4515.159 调用此进程的命令行: ['C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', '--single-argument', 'https://www.cnblogs.com/zhuosanxun/'] 进程PID: 19712 进程的父PID: 15460 进程的子进程: [psutil.Process(pid=18616, name='chrome.exe', status='running', started='09:52:55'), psutil.Process(pid=19036, name='chrome.exe', status='running', started='09:52:55'), psutil.Process(pid=21220, name='chrome.exe', status='running', started='09:52:56'), psutil.Process(pid=15644, name='chrome.exe', status='running', started='09:52:56'), psutil.Process(pid=12984, name='chrome.exe', status='running', started='09:53:00'), psutil.Process(pid=15560, name='chrome.exe', status='running', started='09:53:02'), psutil.Process(pid=14640, name='chrome.exe', status='running', started='13:56:57'), psutil.Process(pid=18676, name='chrome.exe', status='running', started='13:57:04'), psutil.Process(pid=17920, name='chrome.exe', status='running', started='14:25:33'), psutil.Process(pid=12860, name='chrome.exe', status='running', started='14:25:33'), psutil.Process(pid=20524, name='chrome.exe', status='running', started='14:25:35'), psutil.Process(pid=13476, name='chrome.exe', status='running', started='14:25:36'), psutil.Process(pid=15248, name='chrome.exe', status='running', started='14:25:40'), psutil.Process(pid=18384, name='chrome.exe', status='running', started='15:22:36'), psutil.Process(pid=18664, name='chrome.exe', status='running', started='15:24:29'), psutil.Process(pid=1264, name='chrome.exe', status='running', started='16:02:34'), psutil.Process(pid=15088, name='chrome.exe', status='running', started='16:03:11'), psutil.Process(pid=5808, name='chrome.exe', status='running', started='16:07:09')] 进程的父进程(对象): None 进程的父进程(列表): [] 进程的状态: running 拥有该进程的用户: ZHUOSANXUN 进程创建的时间: 1630029173.4227092 进程累计的时间: pcputimes(user=39.265625, system=45.015625, children_user=0.0, children_system=0.0) 进程的CPU占用率: 0.0 进程的CPU亲和性: [0, 1, 2, 3, 4, 5, 6, 7] 进程的内存信息: pmem(rss=213168128, vms=106442752, num_page_faults=473883, peak_wset=255324160, wset=213168128, peak_paged_pool=1505928, paged_pool=1368072, peak_nonpaged_pool=122576, nonpaged_pool=87992, pagefile=106442752, peak_pagefile=150011904, private=106442752) 进程的内存占用率: 0.831386514295062 进程映射的内存区域: [pmmap_grouped(path='C:\\Windows\\System32\\locale.nls', rss=806912), pmmap_grouped(path='C:\\Windows\\System32\\Speech\\Common\\sapi.dll', rss=1572864)] 进程的I/O信息: pio(read_count=281720, write_count=319164, read_bytes=206467227, write_bytes=439586421, other_count=243763, other_bytes=4205642) 进程打开的文件信息: [popenfile(path='C:\\Program Files\\Google\\Chrome\\Application\\92.0.4515.159\\icudtl.dat', fd=-1), popenfile(path='C:\\Windows\\Fonts\\consola.ttf', fd=-1)] 进程打开的套接字连接: [pconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='0.0.0.0', port=5353), raddr=(), status='NONE')] 进程使用的线程数: 41 进程打开的线程: [pthread(id=1912, user_time=22.359375, system_time=18.890625), pthread(id=15464, user_time=0.0, system_time=0.0), pthread(id=18828, user_time=0.0, system_time=0.0), pthread(id=12924, user_time=0.0, system_time=0.0), pthread(id=12944, user_time=0.0, system_time=0.0), pthread(id=16400, user_time=0.03125, system_time=0.0625), pthread(id=14348, user_time=0.0, system_time=0.0), pthread(id=13256, user_time=0.015625, system_time=0.0), pthread(id=17872, user_time=0.0, system_time=0.0), pthread(id=19120, user_time=0.03125, system_time=1.25), pthread(id=14588, user_time=1.296875, system_time=6.9375), pthread(id=4480, user_time=0.34375, system_time=0.140625), pthread(id=20760, user_time=6.234375, system_time=7.71875), pthread(id=9800, user_time=0.140625, system_time=0.015625), pthread(id=3492, user_time=0.0, system_time=0.0), pthread(id=16028, user_time=0.0, system_time=0.03125), pthread(id=15512, user_time=0.0, system_time=0.0), pthread(id=4344, user_time=4.15625, system_time=0.390625), pthread(id=20516, user_time=0.09375, system_time=0.703125), pthread(id=17420, user_time=0.015625, system_time=0.21875), pthread(id=18620, user_time=0.0, system_time=0.0), pthread(id=19672, user_time=0.0, system_time=0.0), pthread(id=7412, user_time=0.0, system_time=0.0), pthread(id=19292, user_time=0.0, system_time=0.015625), pthread(id=18252, user_time=0.09375, system_time=0.328125), pthread(id=20220, user_time=0.0, system_time=0.015625), pthread(id=1368, user_time=0.0, system_time=0.015625), pthread(id=17640, user_time=0.0, system_time=0.0), pthread(id=8448, user_time=0.0, system_time=0.0), pthread(id=20396, user_time=0.0, system_time=0.0), pthread(id=3424, user_time=0.015625, system_time=0.0), pthread(id=6448, user_time=0.0, system_time=0.0625), pthread(id=6612, user_time=0.0, system_time=0.0), pthread(id=21288, user_time=0.0, system_time=0.0), pthread(id=18280, user_time=0.0, system_time=0.0), pthread(id=2016, user_time=0.015625, system_time=0.015625), pthread(id=9112, user_time=0.0, system_time=0.0), pthread(id=2860, user_time=0.015625, system_time=0.09375), pthread(id=21312, user_time=0.0, system_time=0.0), pthread(id=19168, user_time=0.0, system_time=0.0), pthread(id=18628, user_time=0.0, system_time=0.0)] 进程执行的上下文切换数量: pctxsw(voluntary=54950437, involuntary=0) 进程的优先级(带参数则可设置优先级): Priority.NORMAL_PRIORITY_CLASS 进程的I/O优先级(带参数则可设置优先级): IOPriority.IOPRIO_NORMAL 进程的环境变量: {'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)','WINDIR': 'C:\\windows'} 进程是否正在运行: True
运行结果已经给我们展示了通过psutil获取的多种进程信息。psutil的功能不止于此,它也提供了操作进程的方法,比如上例中的p.nice()和p.ionice()方法。除此之外,还有下面的这些方法可供调用。
import psutil p = psutil.Process(19712) # 挂起进程 p.suspend() # 恢复进程 p.resume() # 终止进程,Windows上是kill()的别名 p.terminate() # 杀掉进程 p.kill() # 等待进程终止 p.wait()
在介绍完psutil关于进程管理的内容后,利用学到的方法,来实践一个小例子加深印象——杀掉系统中指定名字的所有进程。
import psutil def kill_process_by_name(process_name): pl = psutil.pids() try: for pid in pl: if psutil.Process(pid).name() == process_name: psutil.Process(pid).kill() except Exception as e: print(str(e))
if __name__ == '__main__': kill_process_by_name('cmd.exe')
参考资料
- https://pypi.org/project/psutil/
作者:酌三巡
感谢阅读,如需转载请注明出处!