Merge pull request #309 from greut/cpu-count
plugin: get cpu_count from sched_getaffinity
This commit is contained in:
1
changelog/9.bugfix
Normal file
1
changelog/9.bugfix
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Fix issue of virtualized or containerized environments not reporting the number of CPUs correctly.
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user