diff --git a/conftest.py b/conftest.py index e326c60d71..9b1296bb0b 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,4 @@ +import platform import pytest from pathlib import Path @@ -21,6 +22,24 @@ def pytest_addoption(parser): default=False, help="Enable debug plots during tests", ) + # Users on Linux get fork by default but the tests run with forkserver (the default since Python 3.14) + parser.addoption( + "--mp-context", + default="forkserver" if platform.system() == "Linux" else None, + help="Multiprocessing context used by the tests instead of the default one (forkserver on Linux)", + ) + + +def pytest_configure(config): + mp_context = config.getoption("--mp-context") + if mp_context is not None: + import multiprocessing + import spikeinterface.core.globals as si_globals + + # The default is patched as well so it survives reset_global_job_kwargs() + multiprocessing.set_start_method(mp_context, force=True) + si_globals._default_job_kwargs["mp_context"] = mp_context + si_globals.global_job_kwargs["mp_context"] = mp_context def pytest_collection_modifyitems(config, items): @@ -32,6 +51,9 @@ def pytest_collection_modifyitems(config, items): rootdir = Path(config.rootdir) modules_location = rootdir / "src" / "spikeinterface" for item in items: + if config.getoption("--mp-context") is not None and item.name == "test_global_job_kwargs": + item.add_marker(pytest.mark.skip(reason="--mp-context changes the default job kwargs")) + try: rel_path = Path(item.fspath).relative_to(modules_location) except: