Introduction to Worker Farm in Node.js
Worker-farm is a module in Node.js that allows you to easily distribute processing tasks across multiple Node processes. This is exceptionally useful for CPU-intensive tasks that would otherwise block the event loop. By offloading work to separate processes, you can improve application performance and responsiveness.
Installation
npm install worker-farm
API Examples
1. Basic Setup
The minimal setup for worker-farm includes initializing the module and creating workers to handle specific tasks.
const workerFarm = require('worker-farm'); const workers = workerFarm(require.resolve('./worker')); workers('task_data', (err, result) => { if (err) console.error(err); console.log(result); workerFarm.end(workers); });
2. Passing Parameters
You can pass multiple parameters to the worker function easily.
workers('param1', 'param2', (err, result) => { if (err) console.error(err); console.log(result); });
3. Handling Multiple Tasks
Using an array or list of tasks, you can distribute work across multiple workers.
const tasks = ['task1', 'task2', 'task3']; tasks.forEach(task => { workers(task, (err, result) => { if (err) console.error(err); console.log(result); }); });
4. Limiting the Number of Workers
You can control the number of worker processes to maintain system performance.
const options = { maxConcurrentWorkers: 2 }; const workers = workerFarm(options, require.resolve('./worker'));
Complete App Example
Below is a complete example of an application using worker-farm to perform parallel computations.
// main.js const workerFarm = require('worker-farm'); const myWorker = workerFarm(require.resolve('./myWorker')); const tasks = [1, 2, 3, 4, 5]; tasks.forEach(task => { myWorker(task, (err, result) => { if (err) console.error(err); console.log('Result:', result); if (task === tasks.length) workerFarm.end(myWorker); }); }); // myWorker.js module.exports = (task, callback) => { const result = task * 2; // Example computation callback(null, result); };
By following this approach, you can ensure your Node.js applications are efficient and can handle more tasks concurrently.
Hash: c79450e7e75ea657a8efac017c795aafde1cfb5d574262d635d32fb97dbd6c84