【问题标题】:psutil Process cpu percent greater than 100psutil 进程 cpu 百分比大于 100
【发布时间】:2018-07-28 12:50:48
【问题描述】:

所以,我正在创建一个监控函数来监控基准测试过程。

这是函数

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)

我得到了我正在监控的函数的 cpu 使用率,但是 cpu percent()/number of cpus 大于 100,我不明白发生了什么。

reason why i have divided by number of cpus is given in this post

【问题讨论】:

    标签: python python-3.x multiprocessing psutil


    【解决方案1】:

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

    返回一个浮点数,以百分比表示进程 CPU 利用率,如果进程在不同 CPU 上运行多个线程,该值也可以 > 100.0。

    【讨论】:

    • 是的,没错。但是cpu_percent除以cpu个数必须有percent
    【解决方案2】:

    我有一个类似的问题,CPU 使用率在 300% 左右,这是没有意义的。通过增加间隔时间解决了这个问题。这是我如何做到的一个例子:

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

    【讨论】:

      猜你喜欢
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      相关资源
      最近更新 更多