Skip to content

Commit

Permalink
fix #973: cpu_percent() may raise ZeroDivisionError.
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 7, 2017
1 parent fe994a7 commit e5a3298
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
**Bug fixes**

- 971_: [Linux] sensors_temperatures() didn't work on CentOS 7.
- 973_: cpu_percent() may raise ZeroDivisionError.

5.1.2
=====
Expand Down
8 changes: 6 additions & 2 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1751,8 +1751,12 @@ def calculate(t1, t2):

busy_delta = t2_busy - t1_busy
all_delta = t2_all - t1_all
busy_perc = (busy_delta / all_delta) * 100
return round(busy_perc, 1)
try:
busy_perc = (busy_delta / all_delta) * 100
except ZeroDivisionError:
return 0.0
else:
return round(busy_perc, 1)

# system-wide usage
if not percpu:
Expand Down

0 comments on commit e5a3298

Please sign in to comment.