Simplifies forking of worker processes and distributing tasks between them.
Simplifies spawning pool of worker processes and distributing tasks between them.
In master process:
Forker = require'worker-forker'Forker;// Create forker. Specify worker file.var forker = __dirname + "/worker.js";// Spawn some worker processes.forkerspawnrequire'os'cpuslength;// Add some task parameters which would be evenly distributed between workers.forkeraddTasks4 8 15 16;// Add some more tasksforkeraddTask23;forkeraddTask42;// Handle various errorsforkeron'error'console.log"Got error" err;;// Wait until all tasks finishedforkerfinishconsole.log"All tasks finished!";;
In worker file (worker.js in this example):
var Worker = require'../index.js'Worker;// Init worker with function which executes tasks.var worker =setTimeoutconsole.log"Worker got task:" taskParams;callback;1000;;// Start processing tasks.workerstart;