Merge pull request #309 from greut/cpu-count

plugin: get cpu_count from sched_getaffinity
This commit is contained in:
Ronny Pfannschmidt
2018-07-23 20:13:45 +02:00
committed by GitHub
3 changed files with 13 additions and 3 deletions

1
changelog/9.bugfix Normal file
View File

@@ -0,0 +1 @@
Fix issue of virtualized or containerized environments not reporting the number of CPUs correctly.

View File

@@ -31,7 +31,9 @@ def test_dist_options(testdir):
def test_auto_detect_cpus(testdir, monkeypatch): def test_auto_detect_cpus(testdir, monkeypatch):
import os 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) monkeypatch.setattr(os, 'cpu_count', lambda: 99)
else: else:
import multiprocessing import multiprocessing

View File

@@ -5,9 +5,16 @@ import pytest
def parse_numprocesses(s): def parse_numprocesses(s):
if s == 'auto': if s == 'auto':
try: try:
from os import cpu_count from os import sched_getaffinity
except ImportError: except ImportError:
from multiprocessing import cpu_count try:
from os import cpu_count
except ImportError:
from multiprocessing import cpu_count
else:
def cpu_count():
return len(sched_getaffinity(0))
try: try:
n = cpu_count() n = cpu_count()
except NotImplementedError: except NotImplementedError: