简体   繁体   English

获取 python 中的进程 CPU 使用率百分比

[英]Get process CPU usage in percentage in python

I'm making an destktop application like task manager.我正在制作一个像任务管理器这样的桌面应用程序。 How to get the specific cpu usage of google.exe?如何获取google.exe的具体cpu使用情况?

You can use psutil library for your task您可以将psutil 库用于您的任务

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())

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但是你应该计算这个过程的总和

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM