Skip to content

Commit

Permalink
FIX n_jobs=None behavior in Parallel and config contexts (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiedbb committed Jul 10, 2023
1 parent 8be8d32 commit 4ccc02d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Latest changes
==============

Release 1.3.2 -- In developpement
---------------------------------

- Fix a regression in ``joblib.Parallel`` introduced in 1.3.0 where
explicitly setting ``n_jobs=None`` was not interpreted as "unset".
https://github.com/joblib/joblib/pull/1475

Release 1.3.1 -- 2023/06/29
---------------------------

Expand Down
4 changes: 4 additions & 0 deletions joblib/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,10 @@ def __init__(
prefer=default_parallel_config["prefer"],
require=default_parallel_config["require"],
):
# Interpret n_jobs=None as 'unset'
if n_jobs is None:
n_jobs = default_parallel_config["n_jobs"]

active_backend, context_config = _get_active_backend(
prefer=prefer, require=require, verbose=verbose
)
Expand Down
26 changes: 26 additions & 0 deletions joblib/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,29 @@ def test_threadpool_limitation_in_child_context_error(context, backend):

with raises(AssertionError, match=r"does not acc.*inner_max_num_threads"):
context(backend, inner_max_num_threads=1)


@parametrize("context", [parallel_config, parallel_backend])
def test_parallel_n_jobs_none(context):
# Check that n_jobs=None is interpreted as "unset" in Parallel
# non regression test for #1473
with context(backend="threading", n_jobs=2):
with Parallel(n_jobs=None) as p:
assert p.n_jobs == 2

with context(backend="threading"):
default_n_jobs = Parallel().n_jobs
with Parallel(n_jobs=None) as p:
assert p.n_jobs == default_n_jobs


@parametrize("context", [parallel_config, parallel_backend])
def test_parallel_config_n_jobs_none(context):
# Check that n_jobs=None is interpreted as "explicitly set" in
# parallel_(config/backend)
# non regression test for #1473
with context(backend="threading", n_jobs=2):
with context(backend="threading", n_jobs=None):
# n_jobs=None resets n_jobs to backend's default
with Parallel() as p:
assert p.n_jobs == 1

0 comments on commit 4ccc02d

Please sign in to comment.