Skip to content
Merged
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
18 changes: 16 additions & 2 deletions autofit/non_linear/parallel/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,25 @@ def fork_context():
restore old behaviour. This helper reproduces the pre-3.14 default on
every platform.

On macOS and Windows the ``multiprocessing`` module itself is returned
rather than ``multiprocessing.get_context()``. Both expose the same
``Process`` / ``Queue`` / ``Pool`` API, but calling ``get_context()`` with
no argument *fixes* the interpreter's default start method, and this
function runs at import time (``class Process(fork_context().Process)``).
That made ``multiprocessing.set_start_method("fork")`` raise
``RuntimeError: context has already been set`` in any script or test
suite that imports autofit before choosing its start method (the autofit
test suite does exactly this on macOS in ``test_autofit/conftest.py``).
The module-level API defers the choice until a process actually starts,
which is the pre-3.14 behaviour this helper exists to preserve.

Returns
-------
The "fork" multiprocessing context on POSIX platforms other than macOS,
else the platform default ("spawn" on Windows and macOS).
else the ``multiprocessing`` module, which dispatches to whichever start
method is current when a process is created ("spawn" by default on
Windows and macOS).
"""
if sys.platform != "darwin" and "fork" in multiprocessing.get_all_start_methods():
return multiprocessing.get_context("fork")
return multiprocessing.get_context()
return multiprocessing
Loading