piscina
Fast Node.js worker pool that runs CPU-bound tasks on worker threads with a simple run API.
Official Documentation ↗piscina v4.x / Node 18 LTS(需 worker_threads 支持). Check the official docs for the latest API surface.Installation
npm install piscinapnpm add piscinayarn add piscinaMethods
No methods match this filter.
Overview
Piscina runs CPU-heavy work on a pool of worker threads so the main event loop stays responsive. It handles scheduling, back-pressure and data transfer, exposing results as promises.
Core Methods
Point the pool at a worker module, then call run with an input and an optional name for the exported function. Configuration controls minThreads, maxThreads and idle timeouts.
Usage Notes
Use it for pure computation such as hashing, image math or parsing large files. Keep worker functions free of shared state, because each thread has its own memory space.
Common Pitfalls
Sending large objects clones them unless transferred, which can erase the speedup. Workers add serialization overhead for tiny tasks, so the pool is only worth it above a certain workload size.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/piscinajs/piscina@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:44.600Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- If the task is aborted, the returned Promise is rejected with an error as well. (https://cdn.jsdelivr.net/gh/piscinajs/piscina@latest/README.md)
- Method: destroy() Stops all Workers and rejects all Promises for pending tasks. (https://cdn.jsdelivr.net/gh/piscinajs/piscina@latest/README.md)
- Event: ‘error’ An ‘error’ event is emitted by instances of this class when: - Uncaught exceptions occur inside Worker threads that do not currently handle tasks. (https://cdn.jsdelivr.net/gh/piscinajs/piscina@latest/README.md)
FAQ
- When should I use piscina instead of worker_threads directly?
- Piscina manages pool sizing, queuing and transfer of data between the main thread and workers. Use it to avoid writing that orchestration yourself for CPU-bound work.
- How are large buffers passed to workers?
- Pass ArrayBuffers in a transferList option to move ownership without copying, which is important for multi-megabyte payloads. Otherwise the data is cloned.
- Can tasks be aborted?
- Piscina supports an AbortSignal for queued or running tasks. Aborting a running thread task terminates that worker, so keep tasks resumable or short when cancellation must be safe.
Report a problem with this API docOpens a pre-filled issue on GitHub.