I need to get the particular process cpu usage from task manager using python.
How can i achieve this?
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.