Skip to content

Commit be27cf8

Browse files
authored
BUG: Fix bug with not short-circuiting n_jobs=1 (#13147)
1 parent 7c531e4 commit be27cf8

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

mne/parallel.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ def run_verbose(*args, verbose=logger.level, **kwargs):
128128

129129
my_func = delayed(run_verbose)
130130

131+
# if we got that n_jobs=1, we shouldn't bother with any parallelization
132+
if n_jobs == 1:
133+
# TODO: Hack until https://github.com/joblib/joblib/issues/1687 lands
134+
try:
135+
backend_repr = str(parallel._backend)
136+
except Exception:
137+
backend_repr = ""
138+
is_local = any(
139+
f"{x}Backend" in backend_repr
140+
for x in ("Loky", "Threading", "Multiprocessing")
141+
)
142+
if is_local:
143+
my_func = func
144+
parallel = list
145+
131146
if total is not None:
132147

133148
def parallel_progress(op_iter):

mne/tests/test_filter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,14 +418,21 @@ def test_resample_scipy():
418418

419419

420420
@pytest.mark.parametrize("n_jobs", (2, "cuda"))
421-
def test_n_jobs(n_jobs):
421+
def test_n_jobs(n_jobs, capsys):
422422
"""Test resampling against SciPy."""
423+
joblib = pytest.importorskip("joblib")
423424
x = np.random.RandomState(0).randn(4, 100)
424425
y1 = resample(x, 2, 1, n_jobs=None)
425426
y2 = resample(x, 2, 1, n_jobs=n_jobs)
426427
assert_allclose(y1, y2)
427-
y1 = filter_data(x, 100.0, 0, 40, n_jobs=None)
428-
y2 = filter_data(x, 100.0, 0, 40, n_jobs=n_jobs)
428+
capsys.readouterr()
429+
with joblib.parallel_config(backend="loky", n_jobs=1):
430+
y1 = filter_data(x, 100.0, 0, 40, n_jobs=None, verbose=True)
431+
out, err = capsys.readouterr()
432+
# if it's in there, we didn't triage properly
433+
assert "Parallel(" not in out
434+
assert "Parallel(" not in err
435+
y2 = filter_data(x, 100.0, 0, 40, n_jobs=n_jobs, verbose=True)
429436
assert_allclose(y1, y2)
430437

431438

0 commit comments

Comments
 (0)