简体   繁体   English

如何使用psutil.get_cpu_percent()?

[英]How to use psutil.get_cpu_percent()?

How to exactly use the function get_cpu_percent()? 如何正确使用函数get_cpu_percent()?

My code is: 我的代码是:

SDKTestSuite.DijSDK_CalculateFps(int(timeForFPS),int(index),cameraName)
cpuUsage = process.get_cpu_percent()

Here I am calling a Function called SDKTestSuite.DijSDK_CalculateFps() and I am calling get_cpu_percent() to get the CPU usage of this call. 这里我调用一个名为SDKTestSuite.DijSDK_CalculateFps()的函数,我调用get_cpu_percent()来获取此调用的CPU使用率。 I am Calling this function for different inputs, The result is sometimes the CPU usage gives 0.0% which is not expected. 我正在为不同的输入调用此函数,结果有时CPU使用0.0% ,这是预期的。

So Am I using the get_cpu_percent in the correct manner? 那么我是否以正确的方式使用get_cpu_percent How to exactly use this get_cpu_percent function? 如何正确使用这个get_cpu_percent函数? Is there any interval parameters vary here? 这里是否有任何间隔参数变化?

In the actual definition of this function it just sleeps for the given interval and compares the CPU time, but how does it calculates my functionality call here? 在这个函数的实际定义中,它只是在给定的时间间隔内休眠并比较CPU时间,但它如何计算我的函数调用?

If you read the docs , psutil.cpu_percent will: 如果您阅读文档psutil.cpu_percent将:

Return a float representing the current system-wide CPU utilization as a percentage… When interval is 0.0 or None compares system CPU times elapsed since last call or module import, returning immediately… 以百分比形式返回表示当前系统范围CPU利用率的浮点数...当interval为0.0或None时,比较自上次调用或模块导入后经过的系统CPU时间,立即返回...

I'm pretty sure that's not what you want. 我很确定这不是你想要的。

First, if you want to know the CPU usage during a specific call , you have to either (a) call it before and after the function, or (b) call it from another thread or process running in parallel with the call, and for the same time as the call (by passing an interval ). 首先,如果您想知道特定呼叫期间的CPU使用率,您必须(a)在函数之前和之后调用它,或者(b)从与调用并行运行的另一个线程或进程调用它,以及与呼叫同时(通过interval )。

Second, if you want to know how much CPU time that call is using, as opposed to how much that call plus everything else being done by every program on your computer are using, you're not even calling the right function, so there's no way to get it to do what you want. 其次,如果你想知道调用使用多少CPU时间,而不是调用多少以及计算机上每个程序正在使用的其他所有内容,你甚至都没有调用正确的函数,所以没有让它做你想做的事的方式。

And there's nothing in psutil that's designed to do that. psutil中没有任何psutil可以用来做到这一点。 The whole point of psutil is that it provides information about your system, and about your program from the "outside" perspective. psutil在于它从“外部”角度提供有关系统和程序的信息。 It doesn't know anything about what functions you ran when. 它不知道你运行什么功能。

However, there are things that come with the stdlib that do things like that, like resource.getrusage or the cProfile module. 但是,有一些stdlib附带的东西可以做类似的事情,比如resource.getrusagecProfile模块。 Without knowing exactly what you're trying to accomplish, it's hard to tell you exactly what to do, but maybe if you read those linked docs they'll give you some ideas. 如果不确切地知道你想要完成什么,很难告诉你到底要做什么,但也许如果你阅读那些链接的文档,他们会给你一些想法。

you need psutil 你需要psutil

pip install psutil

then 然后

import psutil,os
p = psutil.Process(os.getpid())
print(p.cpu_percent())

os.getpid - this will return the PID of the program running this code. os.getpid - 这将返回运行此代码的程序的PID。 For example, when you run it on pythonw.exe, it will return PID of pythonw.exe. 例如,当您在pythonw.exe上运行它时,它将返回pythonw.exe的PID。 Hence p.get_cpu_percent will return CPU usage of that process. 因此p.get_cpu_percent将返回该进程的CPU使用率。

if you want system wide CPU, psutil.cpu_percent can do the job. 如果你想要系统范围的CPU, psutil.cpu_percent可以完成这项工作。

Hope this helps, Cheers! 希望这会有所帮助,干杯!

Thanks all... I got the solution for my query. 谢谢大家......我得到了我的查询解决方案。

>>> p = psutil.Process(os.getpid())
>>> # blocking
>>> p.get_cpu_percent(interval=1)
2.0
>>> # non-blocking (percentage since last call)
>>> p.get_cpu_percent(interval=0)
2.9

Here the interval value matters a lot. 间隔值非常重要。 The final call will give the usage of cpu in percentage for my actual function call. 最后的调用将为我的实际函数调用提供cpu的百分比用法。

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

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