plugin: get cpu_count from sched_getaffinity

Signed-off-by: Yoan Blanc <yoan.blanc@exoscale.ch>
This commit is contained in:
Yoan Blanc
2018-07-18 08:02:53 +02:00
parent 06593498cd
commit 04882074e1
3 changed files with 12 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):
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

View File

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