【问题标题】:How to get current disk IO and network IO into percentage using Python?如何使用 Python 获取当前磁盘 IO 和网络 IO 的百分比?
【发布时间】:2018-08-27 17:07:12
【问题描述】:

我想编写一个 Python 代码来读取磁盘 IO 和网络 IO 的百分比,就像我们在 Windows 任务管理器中看到的那样。目前我正在使用psutil.disk_io_counters()psutil.net_io_counters()。通过这个,我得到 byte read & byte write 用于磁盘 IO 和 byte received & byte sent 用于网络 IO .但我不知道如何将它们转换为百分比。 还观察到disk_io_counters()net_io_counters() 没有给出实例值。我已经尝试过link 中提供的建议。但我没有得到想要的价值。我的操作系统是 Windows,但我希望脚本以独立于平台的方式使用。那么,如果不安装任何工具,如iotopiostat,是否可以获得我需要的值?我试过以下代码:

import psutil, os
print('Disk: ',psutil.disk_io_counters())
print('Network: ',psutil.net_io_counters())

我还尝试了以下代码来检查 psutil.io_counters() 的使用是否提供了即时磁盘 IO。代码是:

import psutil
import time
for x in range(10):
    for proc in psutil.process_iter():
        io_counters = proc.io_counters() 
        disk_usage_process = io_counters[2] + io_counters[3] # read_bytes + write_bytes
        print("PID: ", proc.pid, "Disk", disk_usage_process)
    print('************************************************************')
    time.sleep(1)

但我观察到这些值不是那个时候的。为了证明我已经使用io.counters() 收集了两个系统进程的磁盘 IO,并且看到它在一段时间后以递增的顺序变化。这意味着它正在从进程的开头添加磁盘 IO。以下是截图:

PID:  10068 Disk 1597555    PID:  8608 Disk 99729700
PID:  10068 Disk 1597555    PID:  8608 Disk 99729828    
PID:  10068 Disk 1597555    PID:  8608 Disk 99729956
PID:  10068 Disk 1597555    PID:  8608 Disk 99730212
PID:  10068 Disk 1598271    PID:  8608 Disk 99730340
PID:  10068 Disk 1598271    PID:  8608 Disk 99730596
PID:  10068 Disk 1598271    PID:  8608 Disk 99730724
PID:  10068 Disk 1598271    PID:  8608 Disk 99730852
PID:  10068 Disk 1598271    PID:  8608 Disk 99731108
PID:  10068 Disk 1598271    PID:  8608 Disk 99731236

【问题讨论】:

  • 将disk_io计数除以磁盘大小和网络字节数除以您的网络容量是一个合理的衡量标准吗?您可以将获得的值与任务管理器中显示的值进行比较以确保
  • 您的问题似乎与stackoverflow.com/questions/31860163/… 重复。这是你要找的吗?
  • @raghav710 是的,之前已经提出过这个问题,我也通过提供链接在我的问题中提到了它。但是,在该问题中提供的答案是不够的。因为在这种情况下,磁盘 io 和网络 io 的值不属于该实例。我的意思是当下的价值。没有答案。
  • @raghav710 你能告诉我如何获取磁盘大小吗?还有我的网络容量...
  • @raghav710 抱歉,我误解了这个问题。所以你现在想要你的 disk_io 占 disk_io 的百分比?

标签: python psutil disk-io


【解决方案1】:

问题

获取进程的磁盘使用率和网络使用率占总磁盘和网络使用率的百分比目前

解决方案

根据我手动检查的结果(在 Windows 上,使用给定的步骤 here),yourProcess.io_counters()read_byteswrite_bytes 值应该为您提供进程的磁盘 I/O。结合使用 psutil.disk_io_counters() 的总 I/O 应该可以得到百分比

所以您获取磁盘使用百分比的代码如下所示

 p = psutil.Process()
 io_counters = p.io_counters() 
 disk_usage_process = io_counters[2] + io_counters[3] # read_bytes + write_bytes
 disk_io_counter = psutil.disk_io_counters()
 disk_total = disk_io_counter[2] + disk_io_counter[3] # read_bytes + write_bytes
 print("Disk", disk_usage_process/disk_total * 100)

但对于网络使用来说,任务似乎并不那么容易。如前所述here

AFAIK 大多数(全部?)操作系统不公开这些指标,所以 不,很遗憾,这(psutil.net_io_counters 的进程版本)是不可能的

更新:尝试循环运行

>>> p = psutil.Process()
>>> for i in range(10):
...   p.io_counters()
...
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)

【讨论】:

  • 感谢您的回答。但是我已经尝试了您的代码,并且此代码仅提供了当前进程的磁盘 io。不是整个系统盘io。它也不是实例值。如果你在 for 循环中使用你的代码,你可以看到磁盘 io 一直在增加。
  • 您能否阐明什么是实例值,以便我改进我的答案?我理解实例值意味着对于当前流程以及答案。
  • 术语实例的意思是在时间 T 测量进程 P 的磁盘 io。如果我在时间 t 测量任何进程的磁盘 io,它将给我确切发生的读写仅在时间 t,并非从时间 0 到 t 发生的所有读取和写入。如果我们在循环内打印磁盘 io,它会增加,即它从时间 0(当进程开始时)到直到 t 取该进程的所有磁盘 io 的总和。但是不知何故,您的回答给了我一个提示,即通过考虑所有正在运行的进程并将时间 t-1 的磁盘 io 减去 t 来计算系统的磁盘 io。谢谢你..
  • @M.P 我有兴趣了解循环时值的变化。我已经用循环打印的值更新了我的答案,我看到该值保持不变。你能帮我理解吗?
  • 对不起,我没有提到循环内的时间延迟。您必须在每次打印后将延迟放入循环中。否则会打印 10 次时间 t 的磁盘。因此,通过延迟 1 秒可确保您每 1 秒后读取该进程的磁盘 io。再次,命令任务将产生一个恒定的磁盘 io,因为它每次都会执行相同数量的读/写。因此,要正确检查这一点,您应该通过延迟时间在循环内打印所有正在运行的进程的磁盘 io。在那里你可以看到进程的磁盘 io 正在增加,因为它从一开始就添加了所有磁盘 io。
猜你喜欢
  • 1970-01-01
  • 2013-11-22
  • 2012-04-28
  • 2013-11-21
  • 1970-01-01
  • 2011-04-26
  • 2014-12-21
  • 1970-01-01
  • 2010-11-21
相关资源
最近更新 更多