Usage

Once installed, import the node-threadpool package. The package contains a single export, Executors.

import { Executors } from "node-threadpool";

Executors contains several methods to instantiate new thread pools. For a full list of methods, see the Executor API documentation.

To create a new thread pool, call one of the methods:

const pool = Executors.newFixedThreadPool(4); // creates a pool with 4 threads

The pool is ready to use and functions can be executed in a worker thread by calling submit(...). For example,

import { Executors } from "node-threadpool";

// creates a thread pool with 4 threads
const pool = Executors.newFixedThreadPool(4);

// these execute in parallel (as long as the pool size >= 2)
const result1 = pool.submit(async () => "done 1");
const result2 = pool.submit(async () => "done 2");

console.log(await result1); // joins and prints "done1"
console.log(await result2); // joins and prints "done2"

submit takes in a function which returns a promise. When the function has been run the promise is resolved and the result is returned.

Important: functions submitted to the thread pool cannot access data outside of the function's context. This includes imported modules which should be imported inside the function.

See the examples section for more examples on how to use the thread pool.