Hi, I am trying to figure out what actually happens when we re-execute the 
master process without killing the child processes of the previous run 
using cluster.
When i re-execute my code, it seems the previous workers also run again and 
sometimes master process is also executed twice.

I am sharing both input and output

   1. var cluster = require('cluster');
   2. var cpus = require('os').cpus().length;
   3. 
   4. if(cluster.isMaster){
   5.   console.log('In Master');
   6.   for(var i = 0; i < 3 ; i++) {
   7.     var worker = cluster.fork();
   8.   }  
   9.   cluster.on('online',function(worker){
   10.     console.log("Worker Live:",worker.id);
   11.   });  
   12.   cluster.workers[1].send("Bazingaa");
   13.   //console.log(cluster.Workers.length);
   14. }
   15. if(cluster.isWorker){
   16.   console.log("In Worker",cluster.worker.id);
   17.   process.on('message',function(msg){
   18.     console.log("Message from server:", msg, cluster.worker.id);
   19.   });
   20.  // cluster.worker.kill();
   21. }
   
  Output:


   1. In Master
   2. Worker Live: 1
   3. Worker Live: 2
   4. Worker Live: 3
   5. In Worker 1
   6. Message from server: Bazingaa 1
   7. In Worker 2
   8. In Worker 3
   9. In Master
   10. Worker Live: 1
   11. Worker Live: 2
   12. Worker Live: 3
   13. In Worker 1
   14. In Worker 2
   15. Message from server: Bazingaa 1
   16. In Master
   17. In Worker 1
   18. Message from server: Bazingaa 1
   19. In Worker 2
   20. In Worker 3

please help me figure out whats happening behind the scenes.

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/5eceb864-ec0a-4831-90db-1d77a6de5a36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to