Daniel Daniel - 7 months ago 63
Python Question

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.

Answer

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.


Note that on newer versions of psutil (>= 2) the Process class has a name method instead of a property and thus you should use:

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