isolation
Subprocess isolation utilities for clean memory measurement.
This module provides utilities for running benchmark measurements in isolated subprocesses to avoid memory pollution from prior allocations.
Classes
IsolatedResult
Result from an isolated subprocess execution.
Attributes
| Name | Type | Description |
|---|---|---|
| error | str | None | — |
| execution_time_s | float | — |
| memory_mb | float | — |
| result | Any | None | — |
| success | bool | — |
Methods
__init__(self, success: bool, result: Any | None = None, error: str | None = None, memory_mb: float = 0.0, execution_time_s: float = 0.0) → NoneParameters
success: boolresult: Any | None= Noneerror: str | None= Nonememory_mb: float= 0.0execution_time_s: float= 0.0
WorkerPool
Manager for parallel worker processes.
Attributes
| Name | Type | Description |
|---|---|---|
| _ctx | — | — |
| _processes | list[] | — |
| _ready_events | list[] | — |
| _result_queue | | None | — |
| _start_event | | None | — |
| context | — | — |
| num_workers | — | — |
Methods
__init__(self, num_workers: int, context: Literal[('spawn', 'fork')] = 'spawn')Initialize worker pool.
Parameters
num_workers: intcontext: Literal[('spawn', 'fork')]= 'spawn'
get_pids(self) → list[int]Get list of worker process IDs.
get_results(self, timeout: float = 120.0) → list[Any]Collect results from all workers.
Parameters
timeout: float= 120.0
get_total_memory_mb(self) → floatGet total memory usage across all workers.
shutdown(self, timeout: float = 10.0) → NoneShutdown all worker processes.
Parameters
timeout: float= 10.0
signal_start(self) → NoneSignal all workers to proceed.
start_workers(self, worker_fn: Callable, worker_args: tuple = {'cls': 'ExprTuple', 'elements': [], 'implicit': False}, worker_kwargs: dict | None = None) → NoneStart worker processes.
Parameters
worker_fn: Callableworker_args: tupleworker_kwargs: dict | None= None
wait_for_ready(self, timeout: float = 300.0) → boolWait for all workers to signal ready.
Parameters
timeout: float= 300.0
Functions
get_dir_size_mb(path: Path) → floatGet total size of directory in MB.
Parameters
path: Path
get_memory_mb(pid: int | None = None) → floatGet process memory usage in MB (RSS).
Parameters
pid: int | None= None
get_total_memory_mb(pids: list[int]) → floatGet total memory usage across multiple processes in MB (RSS).
Parameters
pids: list[int]
measure_memory_in_subprocess(load_fn: Callable[([], Any)], timeout: float = 300.0) → tuple[(float, float, Any)]Measure memory usage of a loading function in an isolated subprocess.
Parameters
load_fn: Callable[([], Any)]timeout: float= 300.0
run_isolated(func: Callable[(..., T)], args: tuple = {'cls': 'ExprTuple', 'elements': [], 'implicit': False}, kwargs: dict | None = None, timeout: float = 300.0, context: Literal[('spawn', 'fork')] = 'spawn') → IsolatedResultRun a function in an isolated subprocess.
Parameters
func: Callable[(..., T)]args: tuplekwargs: dict | None= Nonetimeout: float= 300.0context: Literal[('spawn', 'fork')]= 'spawn'