2

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

5
  • Already answered here: stackoverflow.com/questions/276052/… Commented Dec 26, 2021 at 19:46
  • psutil should be used for this. Commented Dec 26, 2021 at 19:51
  • I want to get specific process usage in percentage ,for example chrome.exe : %25 steam.exe : %15 ....... Commented Dec 26, 2021 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 Commented Dec 26, 2021 at 20:10
  • As in @Eugenij's answer, you can use individual process objects to get their CPU usage. Commented Dec 26, 2021 at 20:15

2 Answers 2

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())
Sign up to request clarification or add additional context in comments.

2 Comments

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 Try to set interval: print(chrome.cpu_percent(interval=0.1))
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.