cpu_percent(interval = None)始终返回0,与间隔值PYTHON无关 [英] cpu_percent(interval=None) always returns 0 regardless of interval value PYTHON
问题描述
无论间隔值如何,代码始终返回0.0值.
The code always returns 0.0 values, regardless of interval values.
import psutil
p = psutil.Process()
print p.cpu_percent(interval=1)
print p.cpu_percent(interval=None)
推荐答案
此行为已记录:
当时间间隔为
0.0
或None
时,会将处理时间与自上次调用以来经过的系统CPU时间进行比较,并立即返回.这意味着第一次调用它会返回一个无意义的0.0
值,您应该忽略它.在这种情况下,为确保准确性,建议第二次调用该函数,两次调用之间的间隔至少为0.1
秒.
When interval is
0.0
orNone
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 meaningless0.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 least0.1
seconds between calls.
还有一个警告,禁止在单个呼叫中使用interval=None
:
There is also a warning against using interval=None
for a single call:
警告::第一次使用 interval =
0.0
或None
调用此函数时,它将返回一个无意义的0.0
值,您应该该值忽略.
Warning: the first time this function is called with interval =
0.0
orNone
it will return a meaningless0.0
value which you are supposed to ignore.
如果使用interval=None
,请确保与以前的呼叫相比要呼叫.cpu_percent
.
If using interval=None
, make sure to call .cpu_percent
compared to a prior call.
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
这篇关于cpu_percent(interval = None)始终返回0,与间隔值PYTHON无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!