Merge pull request #560 from utapyngo/logical-cpu-count

This commit is contained in:
Zac Hatfield-Dodds
2020-08-03 22:49:09 +10:00
committed by GitHub
4 changed files with 14 additions and 34 deletions

1
changelog/553.bugfix.rst Normal file
View File

@@ -0,0 +1 @@
When using ``-n auto``, count the number of physical CPU cores instead of logical ones.

View File

@@ -1,6 +1,12 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
install_requires = ["execnet>=1.1", "pytest>=4.4.0", "pytest-forked", "six"] install_requires = [
"execnet>=1.1",
"psutil>=3.0.0",
"pytest>=4.4.0",
"pytest-forked",
"six",
]
with open("README.rst") as f: with open("README.rst") as f:

View File

@@ -1,31 +1,12 @@
import os
import uuid import uuid
import psutil
import py import py
import pytest import pytest
def auto_detect_cpus(): def auto_detect_cpus():
try: return psutil.cpu_count(logical=False) or psutil.cpu_count() or 1
from os import sched_getaffinity
except ImportError:
if os.environ.get("TRAVIS") == "true":
# workaround https://bitbucket.org/pypy/pypy/issues/2375
return 2
try:
from os import cpu_count
except ImportError:
from multiprocessing import cpu_count
else:
def cpu_count():
return len(sched_getaffinity(0))
try:
n = cpu_count()
except NotImplementedError:
return 1
return n if n else 1
class AutoInt(int): class AutoInt(int):

View File

@@ -35,17 +35,10 @@ def test_dist_options(testdir):
def test_auto_detect_cpus(testdir, monkeypatch): def test_auto_detect_cpus(testdir, monkeypatch):
import os import psutil
from xdist.plugin import pytest_cmdline_main as check_options from xdist.plugin import pytest_cmdline_main as check_options
if hasattr(os, "sched_getaffinity"): monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 99)
monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99)))
elif hasattr(os, "cpu_count"):
monkeypatch.setattr(os, "cpu_count", lambda: 99)
else:
import multiprocessing
monkeypatch.setattr(multiprocessing, "cpu_count", lambda: 99)
config = testdir.parseconfigure("-n2") config = testdir.parseconfigure("-n2")
assert config.getoption("numprocesses") == 2 assert config.getoption("numprocesses") == 2
@@ -59,10 +52,9 @@ def test_auto_detect_cpus(testdir, monkeypatch):
assert config.getoption("numprocesses") == 0 assert config.getoption("numprocesses") == 0
assert config.getoption("dist") == "no" assert config.getoption("dist") == "no"
monkeypatch.delattr(os, "sched_getaffinity", raising=False) monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: None)
monkeypatch.setenv("TRAVIS", "true")
config = testdir.parseconfigure("-nauto") config = testdir.parseconfigure("-nauto")
assert config.getoption("numprocesses") == 2 assert config.getoption("numprocesses") == 1
def test_boxed_with_collect_only(testdir): def test_boxed_with_collect_only(testdir):