-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
`psutil.cpu_percent(interval=None)` and `psutil.cpu_times_percent(interval=None)` are non-blocking functions returning the CPU percent consumption since the last time they were called. In order to do so they use a global variable, in which the last CPU timings are saved, and that means they are not thread safe. E.g., if 10 threads call `cpu_percent(interval=None)` with a 1 second interval, only 1 thread out of 10 will get the right result, as it will "invalidate" the timings for the other 9. Problem can be reproduced with the following script: ```python import threading, time, psutil NUM_WORKERS = psutil.cpu_count() def measure_cpu(): while 1: print(psutil.cpu_percent()) time.sleep(1) for x in range(NUM_WORKERS): threading.Thread(target=measure_cpu).start() while 1: print() time.sleep(1.1) ``` The output looks like this, and it shows how inconsistent CPU measurements are between different threads (notice 0.0 values): ``` 3.5 3.5 0.0 0.0 2.8 2.8 0.0 0.0 2.5 2.5 0.0 0.0 2.5 2.5 2.5 2.5 3.3 3.3 3.3 50.0 2.8 0.0 0.0 0.0 ``` After patch: ``` 0.0 0.0 0.0 0.0 2.0 2.3 2.3 2.3 5.5 5.3 5.5 5.5 3.3 3.3 3.0 3.0 9.0 8.9 9.0 9.4 30.0 30.0 29.6 30.0 24.7 24.7 24.7 24.7 ```
- Loading branch information
Showing
3 changed files
with
46 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters