summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-02-07 21:47:31 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2017-02-07 21:47:31 +0100
commite5a32988815641e0fee816ba4d2c6d779167f4c6 (patch)
tree504404e8a41d81a20af4795d482d7f99983c4780
parentfe994a73a1771897b34562bd0317601a4ad649ea (diff)
downloadpsutil-e5a32988815641e0fee816ba4d2c6d779167f4c6.tar.gz
fix #973: cpu_percent() may raise ZeroDivisionError.release-5.1.3
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/__init__.py8
2 files changed, 7 insertions, 2 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index fd18260b..05bbd936 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -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
=====
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 38822e25..7b539ef6 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1751,8 +1751,12 @@ def cpu_percent(interval=None, percpu=False):
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: