gpt4 book ai didi

python - cpu_percent(interval=None) 无论间隔值 PYTHON 总是返回 0

转载 作者:太空狗 更新时间:2023-10-29 20:22:56 26 4
gpt4 key购买 nike

无论间隔值如何,代码始终返回 0.0 值。

import psutil
p = psutil.Process()
print p.cpu_percent(interval=1)
print p.cpu_percent(interval=None)

最佳答案

此行为是 documented :

When interval is 0.0 or None compares process times to system CPU times elapsed since last call, 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 a second time with at least 0.1 seconds between calls.

还有一条警告不要对单个调用使用 interval=None:

Warning: the first time this function is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore.

如果使用 interval=None,请确保调用 .cpu_percent 与之前的调用相比。

p = psutil.Process(pid=pid)
p.cpu_percent(interval=None)
for i in range(100):
usage = p.cpu_percent(interval=None)
# do other things

代替:

for i in range(100):
p = psutil.Process(pid=pid)
p.cpu_percent(interval=None)
# do other things

关于python - cpu_percent(interval=None) 无论间隔值 PYTHON 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24367424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com