Skip to content

API Reference parrot.tools.repl_worker.pool

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.tools.repl_worker.pool

View source

Worker pool lifecycle: prewarm, TTL eviction, ceiling, crash restart (FEAT-380 Module 4).

WorkerPool sits on top of WorkerHandle (TASK-1941) and owns everything about which worker serves which session, and for how long:

  • Session mapping (G7): one worker per session_id.
  • Prewarm pool (v1, G4/AC10): WorkerConfig.prewarm_pool_size workers started in the background with pandas/numpy/matplotlib already imported, assigned on demand instead of paying the 1-3s import cost on a session's first call.
  • Concurrency ceiling (AC12): max_workers if set, else max(4, cpu_count()) capped at 16. At the ceiling, :meth:acquire raises :class:WorkerPoolExhaustedError immediately — no queueing.
  • Idle TTL (AC12): a session's worker idle longer than idle_ttl_seconds is killed and unmapped; a later acquire for that session spawns fresh (the namespace is intentionally gone).
  • Crash restart: a session whose worker died gets a fresh one on its next acquire — the interrupted call's namespace-loss error was already reported by WorkerHandle.execute() at the time of the crash; this class's job is to make the session usable again, not to resurface that error a second time.
  • Orphan reaping (AC12): :meth:shutdown kills every worker (bound and prewarmed) — zero live workers survive a clean host shutdown on any platform. PR_SET_PDEATHSIG (Linux-only, applied worker-side in worker.py) is the portable backstop for an UNCLEAN host death.

Alias WorkerBootstrapError

Re-exported from parrot.tools.repl_worker.handle.WorkerBootstrapError.

Alias WorkerConfig

Re-exported from parrot.tools.repl_worker.protocol.WorkerConfig.

Alias WorkerHandle

Re-exported from parrot.tools.repl_worker.handle.WorkerHandle.

Class WorkerPool

Prewarmed pool + lifecycle (TTL eviction, ceiling, orphan reaping).

Parameter Type Default Description
config Optional[WorkerConfig] None Resource-limit / lifecycle configuration shared by every worker this pool spawns. Defaults to WorkerConfig().
output_dir Optional[str] None Shared output directory for plots/reports, passed to every spawned :class:WorkerHandle.
repl_kwargs Optional[dict[str, Any]] None Extra PythonREPLTool constructor kwargs mirrored on every worker's internal instance (e.g. return_plot_as_base64, TASK-1943).
executor Optional[concurrent.futures.Executor] None Dedicated executor passed through to every :class:WorkerHandle this pool spawns (code-review fix, AC1). When None (default), each spawned handle creates its own small self-owned executor instead — either way, no worker's blocking I/O ever touches the event loop's shared default executor. Pass PythonREPLTool._repl_executor here to route an entire session's worker I/O through one specific dedicated pool.

Method WorkerPool.memory_summary

Aggregate memory telemetry across every live worker (spec §2).

memory_summary() -> dict[str, int]

Returns: dict[str, int] — {"workers": n, "rss_total": bytes, "host_available": bytes} — rss_total sums each live worker's last observed RSS sample (0 for a worker whose observer has no sample yet, e.g. still booting, or is "unavailable"); host_available is the same cgroup-aware figure the reserve checks use.

Method WorkerPool.restart_count

How many times session_id's worker restarted recently.

Observability only — nothing in the pool branches on this value.

restart_count(session_id: str) -> int

Parameter Type Default Description
session_id str The session to report on.

Returns: int — Restarts within the last _RESTART_WINDOW_S seconds (0 if the session is unknown or has never restarted).

Method WorkerPool.acquire

Return the live worker for session_id, binding/spawning one if needed.

acquire(session_id: str) -> WorkerHandle

Parameter Type Default Description
session_id str Caller-provided session key. One worker per session (G7) — the namespace of one session is never reachable from another.

Returns: WorkerHandle — A live :class:WorkerHandle bound to session_id.

Raises:

  • WorkerPoolExhaustedError — The concurrency ceiling is reached and session_id doesn't already have a live worker.

Method WorkerPool.release

Mark session_id's worker as freshly active (resets its TTL clock).

release(session_id: str) -> None

Parameter Type Default Description
session_id str The session releasing its worker back to idle.

Method WorkerPool.shutdown

Kill every worker (bound and prewarmed) and stop the maintenance loop.

Leaves zero live workers behind, on any platform (AC12).

Code-review fix: previously, a _top_up_prewarmed() background task that was mid-spawn when shutdown() ran could finish afterward and append its freshly-spawned worker into self._prewarmed — a process this method had already "finished" cleaning up and would never kill. self._started = False is now set FIRST (before anything else), so any in-flight top-up observes it (under the lock, both before AND after its own spawn) and kills its own worker instead of resurrecting it into a pool that already reported zero live workers. Tracked background tasks are also cancelled/awaited as a best-effort head start (cancellation can't interrupt a spawn already in flight, hence the belt-and-suspenders _started check).

shutdown() -> None

Class WorkerPoolExhaustedError

Raised by :meth:WorkerPool.acquire when the worker ceiling is reached.

Per spec (AC12): the pool never queues past the ceiling — it rejects immediately with a clear, catchable error naming the current limit.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally