"""Keep heavy jobs (embedding) off reserved cores and at low priority. The foreground game keeps its cores; this process is pinned to the rest and niced down so it yields on any shared core. Linux-only affinity is applied when present. """ from __future__ import annotations import os from collections.abc import Sequence from . import config def isolate( *, allowed: Sequence[int] | None = None, nice_increment: int | None = None, ) -> list[int]: """Pin this process to `allowed` cores and lower its priority. Returns the cores it was pinned to. No-ops the affinity call on platforms without ``os.sched_setaffinity`` (non-Linux). """ cores = list(config.ALLOWED_CORES if allowed is None else allowed) increment = config.NICE_INCREMENT if nice_increment is None else nice_increment set_affinity = getattr(os, "sched_setaffinity", None) if set_affinity is not None: set_affinity(0, set(cores)) if increment and hasattr(os, "nice"): os.nice(increment) return cores