Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround cpu detection on Travis CI #317

Merged
merged 4 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/316.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Workaround cpu detection on Travis CI
5 changes: 5 additions & 0 deletions testing/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def test_auto_detect_cpus(testdir, monkeypatch):
config = testdir.parseconfigure("-nauto")
assert config.getoption('numprocesses') == 99

monkeypatch.delattr(os, 'sched_getaffinity', raising=False)
monkeypatch.setenv('TRAVIS', 'true')
config = testdir.parseconfigure("-nauto")
assert config.getoption('numprocesses') == 2


def test_boxed_with_collect_only(testdir):
from xdist.plugin import pytest_cmdline_main as check_options
Expand Down
39 changes: 24 additions & 15 deletions xdist/plugin.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import os

import py
import pytest


def parse_numprocesses(s):
if s == 'auto':
def auto_detect_cpus():
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 sched_getaffinity
from os import cpu_count
except ImportError:
try:
from os import cpu_count
except ImportError:
from multiprocessing import cpu_count
else:
def cpu_count():
return len(sched_getaffinity(0))
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
try:
n = cpu_count()
except NotImplementedError:
return 1
return n if n else 1


def parse_numprocesses(s):
if s == 'auto':
return auto_detect_cpus()
else:
return int(s)

Expand Down