From 04882074e162283411c4e1be6add4ed94761ce84 Mon Sep 17 00:00:00 2001 From: Yoan Blanc Date: Wed, 18 Jul 2018 08:02:53 +0200 Subject: [PATCH] plugin: get cpu_count from sched_getaffinity Signed-off-by: Yoan Blanc --- changelog/9.bugfix | 1 + testing/test_plugin.py | 4 +++- xdist/plugin.py | 10 ++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 changelog/9.bugfix diff --git a/changelog/9.bugfix b/changelog/9.bugfix new file mode 100644 index 0000000..404609a --- /dev/null +++ b/changelog/9.bugfix @@ -0,0 +1 @@ +Fix issue of virtualized or containerized environments not reporting the number of CPUs correctly diff --git a/testing/test_plugin.py b/testing/test_plugin.py index 5c2b2df..d5c32ba 100644 --- a/testing/test_plugin.py +++ b/testing/test_plugin.py @@ -31,7 +31,9 @@ def test_dist_options(testdir): def test_auto_detect_cpus(testdir, monkeypatch): import os - if hasattr(os, 'cpu_count'): + 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 diff --git a/xdist/plugin.py b/xdist/plugin.py index 8009baa..f172b80 100644 --- a/xdist/plugin.py +++ b/xdist/plugin.py @@ -5,9 +5,15 @@ import pytest def parse_numprocesses(s): if s == 'auto': try: - from os import cpu_count + from os import sched_getaffinity + def cpu_count(): + return len(sched_getaffinity(0)) except ImportError: - from multiprocessing import cpu_count + try: + from os import cpu_count + except ImportError: + from multiprocessing import cpu_count + try: n = cpu_count() except NotImplementedError: