【问题标题】:Get usage disk of a Windows process by name in Python在 Python 中按名称获取 Windows 进程的使用磁盘
【发布时间】:2020-12-25 16:17:22
【问题描述】:

我想创建一个脚本,等待特定进程的磁盘使用量为 0 MB/s。

import time
import psutil


def get_process(name):
    process = None
    for p in psutil.process_iter():
        if name in p.as_dict()["name"].lower():
            process = p
    return process


def wait_process(name):
    process = get_process(name)
    if process:
        while process.disk_usage() > 0:
            time.sleep(5)


wait_process("firefox")

我尝试了 psutil 模块,但似乎我们无法按进程获取磁盘使用情况。

编辑

我试过这个,但我得到了不相关的数字。

def process_disk_usage(process_name: str) -> float:
    # process = None
    for p in psutil.process_iter():
        if process_name.lower() in p.as_dict()["name"].lower():
            r_bytes = p.io_counters()[2]
            w_bytes = p.io_counters()[3]
            t_bytes = r_bytes + w_bytes
            return t_bytes / (1024.0 * 1024.0)
    return None


def wait_process(process_name: str) -> None:
    while process_disk_usage(process_name) > 0:
        time.sleep(5)

【问题讨论】:

    标签: python windows process disk-io


    【解决方案1】:
     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)
    

    在这里回答:How to get current disk IO and network IO into percentage using Python?

    【讨论】:

    • 我无法获得与在 Windows 任务管理器中相同的结果。我想以 MB/s 为单位获取进程磁盘使用情况。
    • @0lan 也许你可以只使用 powershell ? (从python调用powershell脚本)
    猜你喜欢
    • 2020-03-01
    • 2020-03-31
    • 1970-01-01
    • 2019-09-15
    • 2013-04-20
    • 1970-01-01
    • 2015-07-08
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多