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)
Answered here: How to get current disk IO and network IO into percentage using Python?