📄️ Aborting Tasks
Piscina allows submitted tasks to be cancelled even if the task has already been submitted to a worker and is being actively processed. This can be used, for instance, to cancel tasks that are taking too long to run.
📄️ Async Load
Piscina supports asynchronously loaded workers. This feature allows you to perform asynchronous operations during worker initialization, such as loading configurations or establishing database connections.
📄️ Using Typescript with Piscina
Although Piscina itself is written in TypeScript and supports TypeScript out of the box, complication arises when trying to use .ts files directly as worker files because Node.js does not support TypeScript natively.
📄️ ES Module
Piscina supports ES modules (ESM) out of the box. This example demonstrates how to use Piscina with ES modules in both the main script and the worker file.
📄️ Message Port
Worker threads can receive MessagePort objects, enabling direct communication channels with the main thread. This feature is useful for scenarios that require continuous communication or transfer of large data sets between threads.
📄️ Messages
Piscina allows workers to send messages back to the main thread using the parentPort.postMessage() method. This can be useful for sending progress updates, or intermediate results during the execution of a long-running task.
📄️ Move
Piscina provides a move() function that allows the transfer of data between the main thread and worker threads. The example below will show you how to use Piscina.move() to transfer ArrayBuffer without cloning, which can significantly improve performance for large data transfers.
📄️ Multiple Workers
It is possible for a single Piscina pool to run multiple workers at the same time. To do so, pass the worker filename to run method rather than to the Piscina constructor.
📄️ Multiple Workers in One File
Piscina allows you to define multiple worker functions within a single file. This approach can be useful when you have related tasks that you want to keep in one module.
📄️ N-API Native Addon
For CPU-intensive tasks or when implementing workers in languages such as C++ or Rust, you can leverage Piscina's support for native addons as worker implementations.
📄️ Named Tasks
Piscina supports running named tasks within a single worker file. This example demonstrates how to use a dispatcher pattern to execute different operations based on the task name.
📄️ Progress
You can track the progress of long-running tasks using a MessageChannel. This can be adapted for use cases where you need to track the progress of long-running tasks executed in worker threads.
📄️ React Server Side Rendering
This example explains how to use Piscina for server-side rendering (SSR) of React components. We'll compare a pooled version using Piscina with an unpooled version to highlight the benefits of using a thread pool for SSR.
📄️ Resource Limits
Piscina allows you to set resource limits on worker threads to prevent them from consuming excessive memory. The example below will show you how to configure and use resource limits to handle out-of-memory scenarios.
📄️ Scrypt
This example demonstrates the performance benefits of using Piscina for CPU-intensive cryptographic operations, specifically the scrypt key derivation function. It compares four different implementations: pooled and unpooled versions, each with both synchronous and asynchronous variants.
📄️ Server
The benefit of offloading work to a worker pool will vary significantly
📄️ Simple
In this example, we create a Piscina instance that uses a worker file to perform a simple addition operation. The main script (index.js) creates the Piscina instance and runs a task, while the worker script (worker.js) defines the task to be executed.
📄️ Simple Async
This example builds upon the simple addition scenario in the previous section. In this section, we simulated an asynchronous operation in the worker file. The simulated delay (100ms) represents any asynchronous operation that might occur in a real-world scenario, such as database queries, file I/O, or network requests.
📄️ Stream
This example explains how Piscina can be used for more complex scenarios involving stream processing. Piscina allows for efficient data transfer between the main thread and worker threads using MessagePort.
📄️ Stream-In
Piscina can be effectively used in data processing pipelines, handling large volumes of data efficiently by leveraging worker threads and implementing proper flow control.
📄️ Task Queue
You can extend Piscina's functionality by implementing a custom task queue. Instead of the default FIFO (First-In-First-Out) queue, it uses a priority queue to manage tasks.
📄️ Typescript
Although Piscina itself is written in TypeScript and supports TypeScript out of the box, complication arises when trying to use .ts files directly as worker files because Node.js does not support TypeScript natively.
📄️ Web Streams
You can work with modern Web APIs like Web Streams using Piscina. Web Streams enable efficient processing of streaming data across multiple threads. It's particularly useful for scenarios involving large datasets or real-time data processing where the benefits of multi-threading can be significant.
📄️ Web Streams Transfer
Using Web Streams and Piscina, you can create a data processing pipeline. This is useful for cases involving real-time data transformation or analysis.
📄️ Worker Options
Piscina allows you to customize the environment and runtime options for worker threads. You can set environment variables, command-line arguments, and other options for the worker processes.