Skip to content

Commit 24c7c23

Browse files
committed
Changes from code review
1 parent bcb2a5b commit 24c7c23

File tree

7 files changed

+53
-20
lines changed

7 files changed

+53
-20
lines changed

README.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ Install the plugin with::
5757

5858
pip install pytest-xdist
5959

60-
or use the package in develop/in-place mode with
61-
a checkout of the `pytest-xdist repository`_ ::
6260

63-
pip install --editable .
61+
To use ``psutil`` for detection of the number of CPUs available, install the ``psutil`` extra::
62+
63+
pip install pytest-xdist[psutil]
64+
6465

6566
.. _parallelization:
6667

changelog/585.feature.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
New ``pytest_xdist_auto_num_workers`` hook can be implemented by plugins or ``conftets.py`` to control the number of workers when ``--numprocesses=auto`` is given in the command-line.
1+
New ``pytest_xdist_auto_num_workers`` hook can be implemented by plugins or ``conftest.py`` files to control the number of workers when ``--numprocesses=auto`` is given in the command-line.

changelog/585.trivial.rst

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
``psutil`` is no longer a dependency, as it has proven to make ``pytest-xdist`` installation in certain platforms and containers problematic.
1+
``psutil`` has proven to make ``pytest-xdist`` installation in certain platforms and containers problematic, so to use it for automatic number of CPUs detection users need to install the ``psutil`` extra::
22

3-
To those interested to continue to use ``psutil`` to detect the number of CPUs, the new ``pytest_xdist_auto_num_workers`` hook can be used in the root ``conftest.py`` file of the project::
4-
5-
def pytest_xdist_auto_num_workers(config):
6-
import psutil
7-
return psutil.cpu_count(logical=False)
3+
pip install pytest-xdist[psutil]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
platforms=["linux", "osx", "win32"],
1919
packages=find_packages(where="src"),
2020
package_dir={"": "src"},
21-
extras_require={"testing": ["filelock"]},
21+
extras_require={"testing": ["filelock"], "psutil": ["psutil>=3.0"]},
2222
entry_points={
2323
"pytest11": ["xdist = xdist.plugin", "xdist.looponfail = xdist.looponfail"]
2424
},

src/xdist/plugin.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66

77

88
def pytest_xdist_auto_num_workers():
9+
try:
10+
import psutil
11+
except ImportError:
12+
pass
13+
else:
14+
count = psutil.cpu_count(logical=False) or psutil.cpu_count()
15+
if count:
16+
return count
917
try:
1018
from os import sched_getaffinity
19+
20+
def cpu_count():
21+
return len(sched_getaffinity(0))
22+
1123
except ImportError:
1224
if os.environ.get("TRAVIS") == "true":
1325
# workaround https://bitbucket.org/pypy/pypy/issues/2375
@@ -16,11 +28,6 @@ def pytest_xdist_auto_num_workers():
1628
from os import cpu_count
1729
except ImportError:
1830
from multiprocessing import cpu_count
19-
else:
20-
21-
def cpu_count():
22-
return len(sched_getaffinity(0))
23-
2431
try:
2532
n = cpu_count()
2633
except NotImplementedError:

testing/test_plugin.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from contextlib import suppress
2+
13
import py
24
import execnet
35
from xdist.workermanage import NodeManager
46

7+
import pytest
8+
59

610
def test_dist_incompatibility_messages(testdir):
711
result = testdir.runpytest("--pdb", "--looponfail")
@@ -38,6 +42,11 @@ def test_auto_detect_cpus(testdir, monkeypatch):
3842
import os
3943
from xdist.plugin import pytest_cmdline_main as check_options
4044

45+
with suppress(ImportError):
46+
import psutil
47+
48+
monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: None)
49+
4150
if hasattr(os, "sched_getaffinity"):
4251
monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99)))
4352
elif hasattr(os, "cpu_count"):
@@ -67,6 +76,18 @@ def test_auto_detect_cpus(testdir, monkeypatch):
6776
assert config.getoption("numprocesses") == 2
6877

6978

79+
def test_auto_detect_cpus_psutil(testdir, monkeypatch):
80+
from xdist.plugin import pytest_cmdline_main as check_options
81+
82+
psutil = pytest.importorskip("psutil")
83+
84+
monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 42)
85+
86+
config = testdir.parseconfigure("-nauto")
87+
check_options(config)
88+
assert config.getoption("numprocesses") == 42
89+
90+
7091
def test_hook_auto_num_workers(testdir, monkeypatch):
7192
from xdist.plugin import pytest_cmdline_main as check_options
7293

tox.ini

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ envlist=
33
linting
44
py{35,36,37,38,39}-pytestlatest
55
py38-pytestmaster
6+
py38-psutil
67

78
[testenv]
8-
passenv = USER USERNAME
99
extras = testing
1010
deps =
1111
pytestlatest: pytest
1212
pytestmaster: git+https://github.com/pytest-dev/pytest.git@master
1313
commands=
1414
pytest {posargs}
1515

16+
[testenv:py38-psutil]
17+
extras =
18+
testing
19+
psutil
20+
deps = pytest
21+
commands =
22+
pytest {posargs:-k psutil}
23+
1624
[testenv:linting]
17-
skipsdist = True
25+
skip_install = True
1826
usedevelop = True
1927
deps =
2028
pre-commit
@@ -28,9 +36,9 @@ skipsdist = True
2836
usedevelop = True
2937
passenv = *
3038
deps =
31-
towncrier
39+
towncrier
3240
commands =
33-
towncrier --version {posargs} --yes
41+
towncrier --version {posargs} --yes
3442

3543
[pytest]
3644
addopts = -ra

0 commit comments

Comments
 (0)