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

4

28

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.

Kinsley answered 1/5, 2013 at 21:6 Comment(2)
You can't do any better than psutilKreisler
In windows your best bet probably would be performance counters.Tureen
P
44

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.

Penman answered 2/5, 2013 at 5:56 Comment(3)
can psutil get network stats per process? I googled and read docs for about an hour, haven't found anythingCrypt
@Crypt This is unrelated to this question. Please try to search SO for psutil questions about network usage, and after that, if you haven't found a satisfying answer, please go ahead and ask a new question regarding it.In any case you should check Process.connections.Penman
How to know the bandwidth it's using via this moduleJuryman
C
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

Cyanohydrin answered 1/5, 2013 at 21:16 Comment(1)
Thanks, that would be great but it will need to be ran as administrator, is there a way to run it as admin before using it in the command-Line?Kinsley
S
1

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")
Soll answered 10/10, 2013 at 16:32 Comment(0)
I
1

"can psutil get network stats per process? I googled and read docs for about an hour, haven't found anything – chester89"
Don't worry chester89, I've got you covered:

import psutil#God-sent module
for i in range(2):#looping twice given https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent '''Warning the first time this method is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore.'''
    for process0 in psutil.process_iter():#for each process running...
        print(process0.name(),process0.cpu_percent())#...print the name and how much of the overall CPU utilization does this process makes up!
    print('\n'*20)#again, due to the above comment -> "Warning", only the section after this blank space will have useful values!
Invagination answered 24/5, 2023 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.