2

I'm making an destktop application like task manager. How to get the specific cpu usage of google.exe?

Burak
  • 37
  • 4
  • Already answered here: https://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python – CauaSCP Dec 26 '21 at 19:46
  • [`psutil`](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent) should be used for this. – CoderCharmander Dec 26 '21 at 19:51
  • I want to get specific process usage in percentage ,for example chrome.exe : %25 steam.exe : %15 ....... – Burak Dec 26 '21 at 19:51
  • I tried the psutil library but i got total cpu usage.I want to get specific process usage in percentage .Thank you for the answer @CoderCharmander – Burak Dec 26 '21 at 20:10
  • As in @Eugenij's answer, you can use individual process objects to get their CPU usage. – CoderCharmander Dec 26 '21 at 20:15

2 Answers2

1

You can use psutil library for your task

pip install psutil

Usage:

import psutil

chrome = None
for proc in psutil.process_iter():
    if proc.name() == "chrome.exe":
        chrome = proc
        print(chrome.cpu_percent())
Yevhen Bondar
  • 4,357
  • 1
  • 11
  • 31
  • output : 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 Does not seem to get percentage properly on windows. – Burak Dec 26 '21 at 20:18
  • @Burak Try to set interval: `print(chrome.cpu_percent(interval=0.1))` – Yevhen Bondar Dec 26 '21 at 20:24
1

You can use this code:

import psutil

for proc in psutil.process_iter():
    if proc.name() == 'chrome.exe':
        try:
            pinfo = proc.as_dict(attrs=['pid'])
        except psutil.NoSuchProcess:
            pass
        else:
            print(pinfo['pid'])
            p = psutil.Process(pinfo['pid'])
            print(p.cpu_percent(1))

But you should count sum of this process

GermanGerken
  • 11
  • 1
  • 4