Package Exports
- thread-pool-node
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (thread-pool-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Thread Pool
A Thread pool for nodejs worker-threads, which is based on generic-pool.
It relies on the generic-pool mechanism to handle the resources.
Usage Example
// index.js
const createPool = require('thread-pool-node')
const pool = createPool({
workerPath: './path/to/worker.js',
workerOptions: {
workerData: {
magicNumber: 42
}
},
poolOptions: {
min: 2,
max: 4
}
})
const worker = await pool.acquire();
const onMessage = result => {
// do something with worker
doSomeHeavyComputation();
// release back to thread pool
pool.release(worker);
worker.removeListener("message", onMessage);
};
worker.on("message", onMessage);
worker.postMessage(args);
// worker.js
const { parentPort, workerData } = require("worker_threads");
parentPort.on("message", message => {
parentPort.postMessage({ result: workerData.magicNumber })
});
For more info regarding how to configure the pool to your needs, please follow the generic-pool README