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

worker-farm

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

worker-farm

Distribute processing tasks to child processes with an über-simple API and baked-in durability & custom concurrency options.

  • 1.7.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

What is worker-farm?

The worker-farm npm package allows you to distribute processing tasks to multiple child processes, enabling parallel execution and improving performance for CPU-intensive tasks. It is particularly useful for tasks that can be broken down into smaller, independent units of work.

What are worker-farm's main functionalities?

Basic Usage

This code demonstrates the basic usage of worker-farm. It spawns a worker process to handle a task defined in a separate worker file ('./worker'). The result is then logged to the console.

const workerFarm = require('worker-farm');
const workers = workerFarm(require.resolve('./worker'));

workers('someTask', (err, output) => {
  if (err) console.error(err);
  console.log(output);
  workerFarm.end(workers);
});

Handling Multiple Tasks

This code shows how to handle multiple tasks using worker-farm. It iterates over an array of tasks, spawning a worker process for each task and logging the results.

const workerFarm = require('worker-farm');
const workers = workerFarm(require.resolve('./worker'));

const tasks = ['task1', 'task2', 'task3'];

for (let task of tasks) {
  workers(task, (err, output) => {
    if (err) console.error(err);
    console.log(output);
    if (task === tasks[tasks.length - 1]) workerFarm.end(workers);
  });
}

Limiting the Number of Workers

This code demonstrates how to limit the number of concurrent worker processes. In this example, a maximum of 2 workers will be spawned at any given time, even though there are 4 tasks.

const workerFarm = require('worker-farm');
const workers = workerFarm({ maxConcurrentWorkers: 2 }, require.resolve('./worker'));

const tasks = ['task1', 'task2', 'task3', 'task4'];

for (let task of tasks) {
  workers(task, (err, output) => {
    if (err) console.error(err);
    console.log(output);
    if (task === tasks[tasks.length - 1]) workerFarm.end(workers);
  });
}

Other packages similar to worker-farm

Keywords

FAQs

Package last updated on 03 May 2019

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