Skip to content

Commit

Permalink
Changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Aug 25, 2020
1 parent bcb2a5b commit 24c7c23
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
7 changes: 4 additions & 3 deletions README.rst
Expand Up @@ -57,10 +57,11 @@ Install the plugin with::

pip install pytest-xdist

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

pip install --editable .
To use ``psutil`` for detection of the number of CPUs available, install the ``psutil`` extra::

pip install pytest-xdist[psutil]


.. _parallelization:

Expand Down
2 changes: 1 addition & 1 deletion changelog/585.feature.rst
@@ -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.
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.
8 changes: 2 additions & 6 deletions changelog/585.trivial.rst
@@ -1,7 +1,3 @@
``psutil`` is no longer a dependency, as it has proven to make ``pytest-xdist`` installation in certain platforms and containers problematic.
``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::

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::

def pytest_xdist_auto_num_workers(config):
import psutil
return psutil.cpu_count(logical=False)
pip install pytest-xdist[psutil]
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -18,7 +18,7 @@
platforms=["linux", "osx", "win32"],
packages=find_packages(where="src"),
package_dir={"": "src"},
extras_require={"testing": ["filelock"]},
extras_require={"testing": ["filelock"], "psutil": ["psutil>=3.0"]},
entry_points={
"pytest11": ["xdist = xdist.plugin", "xdist.looponfail = xdist.looponfail"]
},
Expand Down
17 changes: 12 additions & 5 deletions src/xdist/plugin.py
Expand Up @@ -6,8 +6,20 @@


def pytest_xdist_auto_num_workers():
try:
import psutil
except ImportError:
pass
else:
count = psutil.cpu_count(logical=False) or psutil.cpu_count()
if count:
return count
try:
from os import sched_getaffinity

def cpu_count():
return len(sched_getaffinity(0))

except ImportError:
if os.environ.get("TRAVIS") == "true":
# workaround https://bitbucket.org/pypy/pypy/issues/2375
Expand All @@ -16,11 +28,6 @@ def pytest_xdist_auto_num_workers():
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:
Expand Down
21 changes: 21 additions & 0 deletions testing/test_plugin.py
@@ -1,7 +1,11 @@
from contextlib import suppress

import py
import execnet
from xdist.workermanage import NodeManager

import pytest


def test_dist_incompatibility_messages(testdir):
result = testdir.runpytest("--pdb", "--looponfail")
Expand Down Expand Up @@ -38,6 +42,11 @@ def test_auto_detect_cpus(testdir, monkeypatch):
import os
from xdist.plugin import pytest_cmdline_main as check_options

with suppress(ImportError):
import psutil

monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: None)

if hasattr(os, "sched_getaffinity"):
monkeypatch.setattr(os, "sched_getaffinity", lambda _pid: set(range(99)))
elif hasattr(os, "cpu_count"):
Expand Down Expand Up @@ -67,6 +76,18 @@ def test_auto_detect_cpus(testdir, monkeypatch):
assert config.getoption("numprocesses") == 2


def test_auto_detect_cpus_psutil(testdir, monkeypatch):
from xdist.plugin import pytest_cmdline_main as check_options

psutil = pytest.importorskip("psutil")

monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 42)

config = testdir.parseconfigure("-nauto")
check_options(config)
assert config.getoption("numprocesses") == 42


def test_hook_auto_num_workers(testdir, monkeypatch):
from xdist.plugin import pytest_cmdline_main as check_options

Expand Down
16 changes: 12 additions & 4 deletions tox.ini
Expand Up @@ -3,18 +3,26 @@ envlist=
linting
py{35,36,37,38,39}-pytestlatest
py38-pytestmaster
py38-psutil

[testenv]
passenv = USER USERNAME
extras = testing
deps =
pytestlatest: pytest
pytestmaster: git+https://github.com/pytest-dev/pytest.git@master
commands=
pytest {posargs}

[testenv:py38-psutil]
extras =
testing
psutil
deps = pytest
commands =
pytest {posargs:-k psutil}

[testenv:linting]
skipsdist = True
skip_install = True
usedevelop = True
deps =
pre-commit
Expand All @@ -28,9 +36,9 @@ skipsdist = True
usedevelop = True
passenv = *
deps =
towncrier
towncrier
commands =
towncrier --version {posargs} --yes
towncrier --version {posargs} --yes

[pytest]
addopts = -ra
Expand Down

0 comments on commit 24c7c23

Please sign in to comment.