Skip to content
Advertisement

How to use psutil.get_cpu_percent()?

How to exactly use the function 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. I am Calling this function for different inputs, The result is sometimes the CPU usage gives 0.0% which is not expected.

So Am I using the get_cpu_percent in the correct manner? How to exactly use this get_cpu_percent function? 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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement