Piscina pools Node.js worker threads to run CPU-heavy tasks in parallel with configurable queueing, cancellation, and resource limits. It supports CommonJS, ESM, and TypeScript worker modules.
Overview
Piscina dispatches tasks to a pool of Node.js Worker threads, each running an exported function from a separate file. The main thread submits work with run(), receives promises for results, and can scale minimum and maximum thread counts, idle timeouts, and queue depth—including an auto queue limit derived from maxThreads.
The library targets fixed and variable workloads where spinning up unbounded workers would waste memory or stall the event loop. It integrates with async tracking, exposes statistics for run and wait times, and supports canceling tasks via AbortSignal or abort EventEmitters. Transferable objects can move buffers without copying when posting tasks.
Workers can export named handlers, delay readiness until async initialization completes, and participate in BroadcastChannel messaging on supported Node versions. Options cover memory resourceLimits per worker, custom FIFO or fixed task queues, backpressure when maxQueue is set, and optional Linux thread nice priorities via an add-on module.
Piscina is written in TypeScript and intended for Node.js versions that include mature worker_threads support. It was created to demonstrate disciplined pooling patterns as worker threads became common in production services.
Key features
- Configurable worker pool sizes
- Task cancellation support
- Run and wait time statistics
- Custom task queue implementations
- Worker memory resource limits
- CommonJS, ESM, and TypeScript workers
- Multiple named worker exports
- BroadcastChannel worker messaging
Use cases
- CPU-bound work off the event loop
- Controlled parallel image or data processing
- Pooled workers with queue backpressure
- Stream-fed worker pipelines