Prevent Multiple Console Logs from Clustering

I am using the cluster module for nodejs .

Here's how I set it up:

 var cluster = require('cluster'); if (cluster.isMaster) { var numCPUs = require('os').cpus().length; for (var i = 0; i < numCPUs; i++) { cluster.fork(); } }else{ console.log("Turkey Test"); } 

Now I impose 6 threads (6 cores) on my computer. So, when debugging my application and reading data from the console, it will look:

Is there a way to output console.log only once, regardless of how many clusters are running?

0
v8 cluster-computing
source share
1 answer

You can use the fact that you can communicate with workers and send a message that tells each employee whether he should register or not. You would send it so that only one worker (the first, for example) should register:

 var cluster = require('cluster'); if (cluster.isMaster) { var numCPUs = require('os').cpus().length; for (var i = 0; i < numCPUs; i++) { cluster.fork().send({doLog: i == 0}); } }else{ process.on('message', function(msg) { if(msg.doLog) { console.log('Turkey Test'); } }); } 
+2
source share

All Articles