thequestioneeeer
thequestioneeeer

Reputation: 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? What does that mean?

Upvotes: 0

Views: 1663

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

From the documentation:

When interval is > 0.0 compares system CPU times elapsed before and after the interval (blocking).

When interval is 0.0 or None compares system CPU times elapsed since last call or module import, 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 it is recommended for accuracy that this function be called with at least 0.1 seconds between calls.

So if you pass interval=1, it will measure CPU utilization over 1 second. The call will block (presumably sleep) whilebthe measurement is taken.

Upvotes: 1

Related Questions