2

I need to get the particular process cpu usage from task manager using python.

How can i achieve this?

1 Answer 1

2

You can use psutil library.

For getting process

import psutil
p = psutil.Process(<ProcessID>)
print(p.cpu_percent(interval=1.0))

This will give you the float representation of the current system-wide CPU utilization as a percentage by a process.

Also if you are having any trouble in retrieving any running process PID you can again use psutil library as :

import psutil
for proc in psutil.process_iter():
    if proc.name() == <Some Running Process Name> :
        try:
            pinfo = proc.as_dict(attrs=['pid'])
        except psutil.NoSuchProcess:
            pass
        else:
            print(pinfo)

It will give you pid inside a dictionary thou.

Sign up to request clarification or add additional context in comments.

1 Comment

I can't match the numbers obtained here with those I find in Process Explorer.

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.