"""Handlers for the isolation tests, in a module a child process can import. Not a test file — pytest does not collect it — and deliberately separate from the tests that use it, because the point of `isolate=True` is that the work runs in a *different process*. That process gets nothing from this one: it imports `pipeline.tests.isolated_handlers` itself, by the name in `--bootstrap`, which is exactly what an app's own bootstrap has to do. It also documents the whole of the contract, which is two lines: import the modules that fill `JOBS`, and return the `(config, store)` pair the handlers take. Here both are dicts, since nothing in the pipeline reads them. """ from __future__ import annotations import os import time from typing import Any from pipeline import NULL, RunContext, job @job("iso_ok", "Working in its own process", isolate=True) def iso_ok(config: Any, store: Any, *, context: RunContext = NULL, n: int = 0) -> dict[str, Any]: """Reports which process it ran in, so a test can prove it was not ours.""" context.progress(n, n, f"counting to {n}") return {"doubled": n * 2, "pid": os.getpid()} @job("iso_raises", "Failing in its own process", isolate=True) def iso_raises(config: Any, store: Any, *, context: RunContext = NULL) -> dict[str, Any]: raise RuntimeError("the model said no") @job("iso_slow", "Taking too long on purpose", isolate=True, timeout=1.0) def iso_slow(config: Any, store: Any, *, context: RunContext = NULL) -> dict[str, Any]: """Uninterruptible on purpose: no `check()`, so only a kill ends it. `time.sleep` stands in for `sklearn.fit` — the difference that matters is not what it does but that nothing in it ever asks whether to stop, which is the case a thread has no answer for. """ time.sleep(120) return {"never": True} def bootstrap() -> tuple[Any, Any]: """What `--bootstrap` names. Importing this module registered the jobs.""" return {"config": "rebuilt in the child"}, {"store": "rebuilt in the child"} # ---------------------------------------------------- two workers, one file @job("mp_mark", "Leaving a mark") def mp_mark(config: Any, store: Any, *, context: RunContext = NULL, i: int = 0) -> dict[str, Any]: """Leave a file named after this job and the process that ran it. A file per run rather than a line in a shared one, because creating a uniquely named file is atomic on every platform and appending from two processes is not. A job run twice leaves two files with the same number. """ import uuid from pathlib import Path (Path(store) / f"{i}-{os.getpid()}-{uuid.uuid4().hex}").touch() time.sleep(0.02) # long enough that both workers get a share return {"i": i} def marking(marks: str) -> tuple[Any, Any]: """The bootstrap `pipeline.worker` is given: the marks directory is the store.""" return {}, marks