Quiero saber si el tiempo de espera se cuenta en psutil.cpu_percent(), así que escriba un código como el siguiente para probar
#cat test.py import os import psutil import time p = psutil.Process(os.getpid()) start = time.time() times_before_workload = p.cpu_times() # this function return cpu_percent between two call. so we have to call it once before workload percent_before_workload = p.cpu_percent() # I call f open/close many times and read from the file # hoping cpu will spent time on both user system and iowait c = 1000 while c: f = open('/tmp/big_text') content = f.read(10240) f.close() c -= 1 end = time.time() percent_after_workload = p.cpu_percent() times_after_workload = p.cpu_times() user_seconds = times_after_workload.user - times_before_workload.user system_seconds = times_after_workload.system - times_before_workload.system iowait_seconds = times_after_workload.iowait - times_before_workload.iowait # if cpu percent == user_percent + system_percent + iowait_percent then it means yes. iowait are counted print 'user_percent %s' % round(user_seconds / (end - start) * 100, 2) print 'system_percent %s' % round(system_seconds / (end - start) * 100, 2) print 'iowait_percent %s' % round(iowait_seconds / (end - start) * 100, 2) print 'percent %s' % percent_after_workloadaquí está esta salida
#python test.py user_percent 67.06 system_percent 67.06 iowait_percent 0.0 percent 134.8El iowait es 0, por lo que aún no se puede verificar. Entonces las preguntas son
Es posible que deba instalar vmtouch:
apt update apt install vmtouchEjecute vmtouch en Python:
import subprocess subprocess.run(['vmtouch', '-e', '/tmp/big_text'], stdout=subprocess.DEVNULL)Referencia: ¿Por qué iowait de lectura del archivo es cero?