{"library":"node-worker-pool","title":"Node Worker Pool","description":"node-worker-pool is a JavaScript library for Node.js that provides a robust mechanism for managing a pool of child worker processes. It's designed for scenarios involving numerous highly parallelizable tasks, utilizing an exclusive message-passing paradigm rather than shared memory for inter-process communication. The library, currently at version 3.0.2, allows developers to define custom worker executables (though helper libraries are currently Node.js-specific) that communicate over `stdin`/`stdout` using a defined protocol. While its release cadence isn't explicitly documented, it focuses on stability and efficient task distribution. Key differentiators include its explicit message-passing design and the ability to use external executables as workers, offering flexibility beyond typical Node.js `worker_threads` when process isolation and custom language workers are desired.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install node-worker-pool"],"cli":null},"imports":["const WorkerPool = require('node-worker-pool');","const workerUtils = require('node-worker-pool/nodeWorkerUtils');","const { startWorker, respondWithError } = require('node-worker-pool/nodeWorkerUtils');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const fs = require('fs');\nconst path = require('path');\nconst WorkerPool = require('node-worker-pool');\n\nconst workerScriptContent = `\nvar workerUtils = require('node-worker-pool/nodeWorkerUtils');\n\nvar initData;\nfunction onInitialize(data) {\n  initData = data;\n}\n\nfunction onMessage(data) {\n  return {\n    initData: initData,\n    receivedData: data,\n    pid: process.pid\n  };\n}\n\nif (require.main === module) {\n  try {\n    workerUtils.startWorker(onInitialize, onMessage);\n  } catch (e) {\n    workerUtils.respondWithError(e);\n  }\n}\n`;\n\n// Create a temporary worker file\nconst workerFilePath = path.join(__dirname, 'temp-worker.js');\nfs.writeFileSync(workerFilePath, workerScriptContent);\n\nasync function runPool() {\n  console.log('Starting worker pool...');\n  const workerPool = new WorkerPool(\n    2, // Use 2 workers for demonstration\n    process.execPath, // Path to the node binary\n    workerFilePath,   // Path to the temporary worker script\n    {\n      initData: { someUsefulConstant: 42 }\n    }\n  );\n\n  try {\n    console.log('Sending messages to workers...');\n    const responses = await Promise.all([\n      workerPool.sendMessage({ message: 'Hello from main 1!' }),\n      workerPool.sendMessage({ message: 'Hello from main 2!' }),\n      workerPool.sendMessage({ message: 'Hello from main 3!' })\n    ]);\n\n    responses.forEach((response, index) => {\n      console.log(`Response ${index + 1}:`, response);\n    });\n\n    console.log('Shutting down worker pool...');\n    await workerPool.shutDown();\n    console.log('All worker processes have now been killed.');\n  } catch (error) {\n    console.error('An error occurred:', error);\n  } finally {\n    // Clean up the temporary worker file\n    fs.unlinkSync(workerFilePath);\n    process.exit(0);\n  }\n}\n\nrunPool();","lang":"javascript","description":"This quickstart demonstrates how to set up a `WorkerPool` with a dynamically created worker script, send messages to the pool, and process responses, showing inter-process communication.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}