diff options
| -rw-r--r-- | HISTORY.rst | 1 | ||||
| -rw-r--r-- | psutil/__init__.py | 8 |
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: |
