Skip to content

Commit

Permalink
fix #1085: cpu_count() return value is now checked and forced to None…
Browse files Browse the repository at this point in the history
… if <= 1
  • Loading branch information
giampaolo committed May 19, 2017
1 parent d2b3066 commit e20b347
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
9 changes: 6 additions & 3 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e20b347

Please sign in to comment.