Skip to content

Commit 7f07d50

Browse files
Merge pull request #646 from kroeschl/numprocesses-logical
Add explicit 'logical' choice to --numprocesses flag
2 parents fb518de + d4d01bb commit 7f07d50

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

changelog/646.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add ``--numprocesses=logical`` flag, which automatically uses the number of logical CPUs available, instead of physical CPUs with ``auto``.
2+
3+
This is very useful for test suites which are not CPU-bound.

src/xdist/plugin.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
import pytest
66

77

8-
def pytest_xdist_auto_num_workers():
8+
def pytest_xdist_auto_num_workers(config):
99
try:
1010
import psutil
1111
except ImportError:
1212
pass
1313
else:
14-
count = psutil.cpu_count(logical=False) or psutil.cpu_count()
14+
use_logical = config.option.numprocesses == "logical"
15+
count = psutil.cpu_count(logical=use_logical) or psutil.cpu_count()
1516
if count:
1617
return count
1718
try:
@@ -36,8 +37,8 @@ def cpu_count():
3637

3738

3839
def parse_numprocesses(s):
39-
if s == "auto":
40-
return "auto"
40+
if s in ("auto", "logical"):
41+
return s
4142
elif s is not None:
4243
return int(s)
4344

@@ -51,9 +52,10 @@ def pytest_addoption(parser):
5152
metavar="numprocesses",
5253
action="store",
5354
type=parse_numprocesses,
54-
help="shortcut for '--dist=load --tx=NUM*popen', "
55-
"you can use 'auto' here for auto detection CPUs number on "
56-
"host system and it will be 0 when used with --pdb",
55+
help="Shortcut for '--dist=load --tx=NUM*popen'. With 'auto', attempt "
56+
"to detect physical CPU count. With 'logical', detect logical CPU "
57+
"count. If physical CPU count cannot be found, falls back to logical "
58+
"count. This will be 0 when used with --pdb.",
5759
)
5860
group.addoption(
5961
"--maxprocesses",
@@ -190,7 +192,7 @@ def pytest_configure(config):
190192
@pytest.mark.tryfirst
191193
def pytest_cmdline_main(config):
192194
usepdb = config.getoption("usepdb", False) # a core option
193-
if config.option.numprocesses == "auto":
195+
if config.option.numprocesses in ("auto", "logical"):
194196
if usepdb:
195197
config.option.numprocesses = 0
196198
config.option.dist = "no"

testing/test_plugin.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def test_auto_detect_cpus(testdir, monkeypatch):
6969
assert config.getoption("numprocesses") == 0
7070
assert config.getoption("dist") == "no"
7171

72+
config = testdir.parseconfigure("-nlogical", "--pdb")
73+
check_options(config)
74+
assert config.getoption("usepdb")
75+
assert config.getoption("numprocesses") == 0
76+
assert config.getoption("dist") == "no"
77+
7278
monkeypatch.delattr(os, "sched_getaffinity", raising=False)
7379
monkeypatch.setenv("TRAVIS", "true")
7480
config = testdir.parseconfigure("-nauto")
@@ -81,12 +87,16 @@ def test_auto_detect_cpus_psutil(testdir, monkeypatch):
8187

8288
psutil = pytest.importorskip("psutil")
8389

84-
monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 42)
90+
monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 84 if logical else 42)
8591

8692
config = testdir.parseconfigure("-nauto")
8793
check_options(config)
8894
assert config.getoption("numprocesses") == 42
8995

96+
config = testdir.parseconfigure("-nlogical")
97+
check_options(config)
98+
assert config.getoption("numprocesses") == 84
99+
90100

91101
def test_hook_auto_num_workers(testdir, monkeypatch):
92102
from xdist.plugin import pytest_cmdline_main as check_options
@@ -101,6 +111,10 @@ def pytest_xdist_auto_num_workers():
101111
check_options(config)
102112
assert config.getoption("numprocesses") == 42
103113

114+
config = testdir.parseconfigure("-nlogical")
115+
check_options(config)
116+
assert config.getoption("numprocesses") == 42
117+
104118

105119
def test_boxed_with_collect_only(testdir):
106120
from xdist.plugin import pytest_cmdline_main as check_options

0 commit comments

Comments
 (0)