Skip to content

Commit

Permalink
Revert "Merge pull request pytest-dev#560 from utapyngo/logical-cpu-c…
Browse files Browse the repository at this point in the history
…ount"

This reverts commit 0094b29, reversing
changes made to c6255fa.
  • Loading branch information
nicoddemus committed Aug 24, 2020
1 parent 1d08799 commit e469fc7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

install_requires = ["execnet>=1.1", "psutil>=3.0.0", "pytest>=6.0.0", "pytest-forked"]
install_requires = ["execnet>=1.1", "pytest>=6.0.0", "pytest-forked"]


with open("README.rst") as f:
Expand Down
23 changes: 21 additions & 2 deletions src/xdist/plugin.py
@@ -1,12 +1,31 @@
import os
import uuid

import psutil
import py
import pytest


def auto_detect_cpus():
return psutil.cpu_count(logical=False) or psutil.cpu_count() or 1
try:
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):
Expand Down
16 changes: 12 additions & 4 deletions testing/test_plugin.py
Expand Up @@ -35,10 +35,17 @@ def test_dist_options(testdir):


def test_auto_detect_cpus(testdir, monkeypatch):
import psutil
import os
from xdist.plugin import pytest_cmdline_main as check_options

monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: 99)
if hasattr(os, "sched_getaffinity"):
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")
assert config.getoption("numprocesses") == 2
Expand All @@ -52,9 +59,10 @@ def test_auto_detect_cpus(testdir, monkeypatch):
assert config.getoption("numprocesses") == 0
assert config.getoption("dist") == "no"

monkeypatch.setattr(psutil, "cpu_count", lambda logical=True: None)
monkeypatch.delattr(os, "sched_getaffinity", raising=False)
monkeypatch.setenv("TRAVIS", "true")
config = testdir.parseconfigure("-nauto")
assert config.getoption("numprocesses") == 1
assert config.getoption("numprocesses") == 2


def test_boxed_with_collect_only(testdir):
Expand Down

0 comments on commit e469fc7

Please sign in to comment.