def check(proc, target_name, max_timeout):
"""
Check CPU usage of target process
"""
try:
pid = proc.pid
start_time = time()
if psutil.Process(pid).children():
for child in psutil.Process(pid).children():
if child.name() == target_name:
while True:
cpu = all(0 == child.cpu_percent(interval=0.1) for x in xrange(8))
if cpu is not None and cpu is True:
kill(proc, child.pid)
break
if max_timeout is not None or max_timeout != 0:
end_time = time()
elapsed = end_time - start_time
if elapsed > max_timeout:
kill(proc, child.pid)
break
except psutil.NoSuchProcess:
pass
评论列表
文章目录