简体   繁体   中英

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. I was hoping I could use ctypes. but I am happy to hear any other options. Thanks for your time.

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 orpsutil.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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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