0

I would like to create a script that wait until a specific process has 0 MB/s of disk usage.

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

I tried the psutil module but it seems we can't get disk usage by process.

EDIT

I tried this but I get irrelevant number.

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)

1 Answer 1

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)

Answered here: How to get current disk IO and network IO into percentage using Python?

Sign up to request clarification or add additional context in comments.

2 Comments

I can't get the same result as in the Windows Task Manager. I would like to get the process disk usage in MB/s.
@0lan Maybe you can just use powershell ? (Call powershell script from python)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.