Python - 在 windows 中获取进程名称、CPU、内存使用情况和峰值内存使用情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16326529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:17:34  来源:igfitidea点击:

Python - get process names,CPU,Mem Usage and Peak Mem Usage in windows

pythonwindowsmemoryprocesscpu

提问by Daniel

I am wanting to get a list of all the process names, CPU, Mem Usage and Peak Mem Usage. I was hoping I could use ctypes. but I am happy to hear any other options. Thanks for your time.

我想获得所有进程名称、CPU、Mem Usage 和 Peak Mem Usage 的列表。我希望我可以使用 ctypes。但我很高兴听到任何其他选择。谢谢你的时间。

采纳答案by Bakuriu

You can use psutil.

您可以使用psutil.

For example, to obtain the list of process names:

例如,要获取进程名称列表:

process_names = [proc.name() for proc in psutil.process_iter()]

For info about the CPU use psutil.cpu_percentor psutil.cpu_times. For info about memory usage use psutil.virtual_memory.

有关 CPU 使用情况的信息psutil.cpu_percentpsutil.cpu_times. 有关内存使用情况的信息,请使用psutil.virtual_memory.

Note that psutil works with Linux, OS X, Windows, Solaris and FreeBSD and with python 2.4 through 3.3.

请注意,psutil 适用于 Linux、OS X、Windows、Solaris 和 FreeBSD 以及 python 2.4 到 3.3。

回答by Kevin D

I like using wmicon Windows. You can run it from the command-line, so you can run it from Python.

我喜欢wmic在 Windows 上使用。您可以从命令行运行它,因此您可以从 Python 运行它。

from subprocess import Popen,PIPE
proc = Popen('wmic cpu',stdout=PIPE, stderr=PIPE)
print str(proc.communicate())

With wmicyou can get processes, cpu, and memory info easily. Just use wmic cpu, wmic process, and wmic memphysical. You can also filter out certain attributes by using wmic <alias> get <attribute>. And you can get a list of all commands with wmic /?. Hope that helps!

有了它,wmic您可以轻松获取进程、CPU 和内存信息。只需使用wmic cpu, wmic process, 和wmic memphysical。您还可以使用 过滤掉某些属性wmic <alias> get <attribute>。您可以使用wmic /?. 希望有帮助!

You can check out the official documentation for WMIC here: http://technet.microsoft.com/en-us/library/bb742610.aspx

您可以在此处查看 WMIC 的官方文档:http: //technet.microsoft.com/en-us/library/bb742610.aspx

回答by Mark

This Python 3.3 code works for Windows 7 with UAC all the way down.

此 Python 3.3 代码适用于带有 UAC 的 Windows 7。

import psutil
import time

def processcheck(seekitem):
    plist = psutil.get_process_list()
    str1=" ".join(str(x) for x in plist)
    if seekitem in str1:
        print ("Requested process is running")   

processcheck("System")