🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

thread-pool-node

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thread-pool-node

Worker threads pool based on generic-pool

latest
npmnpm
Version
1.0.9
Version published
Maintainers
2
Created
Source

Thread Pool

npm version Build Status

A Thread pool for nodejs worker-threads. It relies on the generic-pool library to handle the resource management.

Install

npm install thread-pool-node

or with yarn:

yarn add thread-pool-node

Usage Example

// index.js
const createPool = require('thread-pool-node')

const pool = createPool({
  workerPath: './path/to/worker.js',
  workerOptions: {
    workerData: {
      magicNumber: 42
    }
  },
  poolOptions: { // passed to generic-pool
    min: 2,
    max: 4
  }
})

const worker = await pool.acquire();
const onMessage = result => {
  // do something with the result
  console.log({ result });

  // 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(aCPUBoundTask(workerData.magicNumber))
});

Pool Basic API

  • createPool: Creates a new worker-threads pool according to the given pool options
  • async Pool#acquire - Returns a new ready to be used worker from the pool.
  • async Pool#release - Releases the worker back to the pool.

For info on how to configure the pool to meet your needs, and more useful pool APIs, see generic-pool

Keywords

thread

FAQs

Package last updated on 12 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts