diff --git a/HISTORY.rst b/HISTORY.rst index bdca8f246..a85aacb56 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 a05d62498..c393ecc3a 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -1043,6 +1043,8 @@ def cpu_percent(self, interval=None): 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):