如何使用 WMI 了解进程的 CPU 和内存使用情况? [英] How to know the CPU and memory usage of a process with WMI?

查看:58
本文介绍了如何使用 WMI 了解进程的 CPU 和内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 wmi 和 python 来跟踪在我的机器上运行的进程的行为.

<前>从 win32com.client 导入 GetObjectwmi = GetObject('winmgmts:')进程 = wmi.InstancesOf('Win32_Process')对于进程中的进程:打印 process.ProcessId、process.Name

Win32_Process 有一个大量信息 但我没有看到任何用于跟踪 CPU 消耗的内容.窗口任务监视器正在显示此信息,因此我认为可以获取它.

我认为 WorkingSetSize 属性给出了进程的内存消耗,但我可以看到与 TaskMonitor 给出的值不同的值.

如何获取给定进程的这 2 个值?

更新:任务监视器显示 PrivateWorkingSetSize,它似乎不适用于 Win32_Process.WorkingSetSize 和 PrivateWorkingSetSize 有什么区别?

解决方案

我很确定你想要 WMI perf 类Win32_PerfFormattedData_PerfProc_ProcessWin32_PerfRawData_PerfProc_Process

例如它们的属性 PercentProcessorTimeWorkingSet

请注意,要理解 Perf 类需要付出一些努力.

但是那些 WMI 类应该为您提供您正在寻找的所有信息.

I am using a wmi and python in order to track the behavior of the process running on my machine.

from win32com.client import GetObject
wmi = GetObject('winmgmts:')
processes = wmi.InstancesOf('Win32_Process')

for process in processes:
    print process.ProcessId, process.Name 

The Win32_Process has a lot of information but I don't see anything for tracking the CPU consumption. The window Task Monitor is showing this info so I think it is possible to get it.

I thought that the WorkingSetSize property is giving the memory consumption of the process but I can see different value from what is given by TaskMonitor.

How to get these 2 values for a given process?

Update: Task Monitor shows the PrivateWorkingSetSize which seems to be not available with the Win32_Process. What is the difference betwen WorkingSetSize and PrivateWorkingSetSize?

解决方案

I'm pretty sure you want the WMI perf classes Win32_PerfFormattedData_PerfProc_Process or Win32_PerfRawData_PerfProc_Process

E.g. their properties PercentProcessorTime and WorkingSet

Note that the Perf classes take a bit effort to understand.

But those WMI classes should give you all the info you're looking for.

这篇关于如何使用 WMI 了解进程的 CPU 和内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆