简体   繁体   English

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

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

I am wanting to get a list of all the process names, CPU, Mem Usage and Peak Mem Usage.我想获得所有进程名称、CPU、Mem Usage 和 Peak Mem Usage 的列表。 I was hoping I could use ctypes.我希望我可以使用 ctypes。 but I am happy to hear any other options.但我很高兴听到任何其他选择。 Thanks for your time.谢谢你的时间。

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_percent orpsutil.cpu_times .有关 CPU 的信息,请使用psutil.cpu_percentpsutil.cpu_times For info about memory usage use psutil.virtual_memory .有关内存使用情况的信息,请使用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。

I like using wmic on Windows.我喜欢在 Windows 上使用wmic You can run it from the command-line, so you can run it from Python.您可以从命令行运行它,因此您可以从 Python 运行它。

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

With wmic you can get processes, cpu, and memory info easily.使用wmic您可以轻松获取进程、cpu 和内存信息。 Just use wmic cpu , wmic process , and wmic memphysical .只需使用wmic cpuwmic processwmic memphysical You can also filter out certain attributes by using wmic <alias> get <attribute> .您还可以使用wmic <alias> get <attribute>过滤掉某些属性。 And you can get a list of all commands with wmic /?您可以使用wmic /?获得所有命令的列表wmic /? . . Hope that helps!希望有帮助!

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

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM