Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

workerpool

Package Overview
Dependencies
Maintainers
2
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workerpool

Offload tasks to a pool of workers on node.js and in the browser

  • 9.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is workerpool?

The workerpool npm package is a robust solution for offloading CPU intensive tasks to worker threads. It allows you to create a pool of workers and manage the execution of tasks in parallel, which can significantly improve the performance of Node.js applications that need to handle heavy computational work.

What are workerpool's main functionalities?

Creating a worker pool and executing tasks

This code sample demonstrates how to create a pool of workers and execute a simple 'add' function in a worker thread. The 'exec' method is used to send the task to an available worker and returns a promise that resolves with the result.

const workerpool = require('workerpool');
const pool = workerpool.pool();

function add(a, b) {
  return a + b;
}

pool.exec(add, [5, 7])
  .then(function (result) {
    console.log('Result: ' + result); // prints 12
  })
  .catch(function (err) {
    console.error(err);
  })
  .then(function () {
    pool.terminate(); // terminate all workers when done
  });

Using a worker script

This code sample shows how to use a separate worker script file. The 'pool' method is given the path to a worker script, and the 'exec' method is used to call a specific exported function from that script, passing in any necessary arguments.

const workerpool = require('workerpool');
const pool = workerpool.pool(__dirname + '/myWorker.js');

pool.exec('heavyComputation', [largeDataSet])
  .then(function (result) {
    console.log('Computation result: ' + result);
  })
  .catch(function (err) {
    console.error(err);
  })
  .then(function () {
    pool.terminate();
  });

Handling timeouts

This code sample illustrates how to handle task timeouts. The 'exec' method accepts an options object where you can specify a 'timeout' in milliseconds. If the task does not complete within the specified time, the promise is rejected.

const workerpool = require('workerpool');
const pool = workerpool.pool();

pool.exec('longRunningTask', [inputData], { timeout: 10000 })
  .then(function (result) {
    console.log('Task result: ' + result);
  })
  .catch(function (err) {
    console.error('Task timed out or other error:', err);
  })
  .then(function () {
    pool.terminate();
  });

Other packages similar to workerpool

Keywords

FAQs

Package last updated on 18 Dec 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc