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

我只是一个虾纸丫 提交于 2019-11-30 12:02:43

问题


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.


回答1:


You can use 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 or psutil.cpu_times. For info about memory usage use psutil.virtual_memory.

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




回答2:


I like using wmic on Windows. You can run it from the command-line, so you can run it from 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. 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!

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




回答3:


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

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


来源:https://stackoverflow.com/questions/16326529/python-get-process-names-cpu-mem-usage-and-peak-mem-usage-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!