简体   繁体   English

psutil 进程 cpu 百分比大于 100

[英]psutil Process cpu percent greater than 100

So, I was creating a monitor function to monitor a process for benchmarking.因此,我创建了一个监控功能来监控基准测试的过程。

This is the function这是功能

def monitor(target):
    worker_process = mp.Process(target=target, args=(5, bounds, num_particles, max_iter, None))
    worker_process.start()
    p = psutil.Process(worker_process.pid)
    cpu_percents = []
    while worker_process.is_alive():
      test = p.cpu_percent()
      if test != 0.0:
         cpu_percents.append(test)

    worker_process.join()
    return cpu_percents
cpu_percents = monitor(target=GSO)

i got the cpu usage of the function that i was monitoring, but the cpu percent()/number of cpus was greater than 100, i don't understand what's going on can someone explain.我得到了我正在监控的函数的 CPU 使用率,但是 CPU 百分比()/CPU 数量大于 100,我不明白发生了什么有人可以解释一下。

reason why i have divided by number of cpus is given in this post 这篇文章给出了我除以 CPU 数量的原因

From psutil doc: http://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent来自 psutil 文档: http ://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent

Return a float representing the process CPU utilization as a percentage which can also be > 100.0 in case of a process running multiple threads on different CPUs.返回一个浮点数,以百分比形式表示进程 CPU 利用率,如果进程在不同 CPU 上运行多个线程,则该值也可以 > 100.0。

i had a similar issue with cpu ussage around 300% which makes no sense.我在 cpu 使用率约为 300% 时遇到了类似的问题,这是没有意义的。 The problem was solved by increasing the interval time.问题是通过增加间隔时间来解决的。 Here is an example of how i did it:这是我如何做到的一个例子:

import psutil
import pandas as pd
import time
import multiprocessing


def get_running_aps(interval=20):
    df = pd.DataFrame(columns=['pid', 'name', 'username', 'status', 'cpu_percent'])

    # this is t0 (start of interval)
    for proc in psutil.process_iter(['pid', 'name', 'username', 'status', 'cpu_percent']):
        pass

    # interval time waiting
    for i in range(interval):
        print("#" * (interval - i))
        time.sleep(1)

    # measure a second time, now save the results
    for proc in psutil.process_iter(['pid', 'name', 'username', 'status', 'cpu_percent']):
        df = df.append(proc.info, ignore_index=True)
    
    # divide by the number of cpu's
    df.cpu_percent = df.cpu_percent/multiprocessing.cpu_count()

    df = df.sort_values(['cpu_percent'], ascending=False)
    return df


if __name__ == "__main__":
    df = get_running_aps()
    print(df.head())

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

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