37,720
社区成员
发帖
与我相关
我的任务
分享
7636
####
0.0
####
我到达被启动函数,下面打印进程cpu使用率
0.0
p = psutil.Process(proc.th32ProcessID)
p.get_cpu_percent(interval=0)
print("####")
print(p.get_cpu_percent(interval=0))#第一次调用
两次get_cpu_percent之间要等一段时间, 或者干脆用p.get_cpu_percent(interval=0.5).
psutil文档里说:
psutil.cpu_percent(interval=None, percpu=False) Return a float representing the current system wide CPU utilization as a percentage. When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking). When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately. That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore. In this case is recommended for accuracy that this function be called with at least 0.1 seconds between calls.
另外, psutil有psutil.process_iter()可以列出所有的process, 而且可以跨平台使用.不需要用kernel32的函数(当然, process_iter()可能调用了kernel32).