summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-05-19 19:33:15 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-05-19 19:33:15 +0200
commite20b3473da81d1c4aca3919a895bc940c52ea333 (patch)
treef912cedd2b532de134ce02989e7ac5178e90122f
parentd2b306688b8ab94ef678676f09dde7aa453b6d0c (diff)
downloadpsutil-e20b3473da81d1c4aca3919a895bc940c52ea333.tar.gz
fix #1085: cpu_count() return value is now checked and forced to None if <= 1
-rw-r--r--HISTORY.rst1
-rw-r--r--psutil/__init__.py9
2 files changed, 7 insertions, 3 deletions
diff --git a/HISTORY.rst b/HISTORY.rst
index bdca8f24..a85aacb5 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -68,6 +68,7 @@
Oleksii Shevchuk)
- 1079_: [FreeBSD] net_connections() didn't list locally connected sockets.
(patch by Gleb Smirnoff)
+- 1085_: cpu_count() return value is now checked and forced to None if <= 1.
**Porting notes**
diff --git a/psutil/__init__.py b/psutil/__init__.py
index a05d6249..c393ecc3 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -1043,6 +1043,8 @@ class Process(object):
blocking = interval is not None and interval > 0.0
if interval is not None and interval < 0:
raise ValueError("interval is not positive (got %r)" % interval)
+ # TODO: rarely cpu_count() may return None, meaning this will
+ # break. It's probably wise to fall back to 1.
num_cpus = _NUM_CPUS or cpu_count()
def timer():
@@ -1645,10 +1647,11 @@ def cpu_count(logical=True):
"""
global _NUM_CPUS
if logical:
- _NUM_CPUS = _psplatform.cpu_count_logical()
- return _NUM_CPUS
+ ret = _psplatform.cpu_count_logical()
+ _NUM_CPUS = ret
else:
- return _psplatform.cpu_count_physical()
+ ret = _psplatform.cpu_count_physical()
+ return ret if ret >= 1 else None
def cpu_times(percpu=False):