简体   繁体   English

调用 psutil.cpu_percent(interval=1) 时发生了什么?

[英]What is happening when you call psutil.cpu_percent(interval=1)?

I want to know my cpu utilization and the command seems to be psutil.cpu_percent(interval=1) , but why do I have to say interval=1 ?我想知道我的 cpu 利用率,命令似乎是psutil.cpu_percent(interval=1) ,但为什么我必须说interval=1 What does that mean?这意味着什么?

From the documentation :文档

When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking).interval > 0.0比较间隔(阻塞)前后经过的系统 CPU 时间。

When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately.interval0.0None比较自上次调用或模块导入以来经过的系统 CPU 时间,立即返回。 That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore.这意味着第一次调用它时,它会返回一个你应该忽略的无意义的 0.0 值。 In this case it is recommended for accuracy that this function be called with at least 0.1 seconds between calls.在这种情况下,为了准确起见,建议在两次调用之间至少间隔 0.1 秒来调用此函数。

So if you pass interval=1 , it will measure CPU utilization over 1 second.因此,如果您传递interval=1 ,它将在 1 秒内测量 CPU 利用率。 The call will block (presumably sleep) whilebthe measurement is taken.在进行测量时,调用将阻塞(大概是休眠)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM