I am having trouble, understanding, why would half of the workers I create are able to read the contents of a file and half of them read nothing.
Here is the code I have:
// Declare initial stuff
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
var words = fs.readFileSync("./wordlist").toString().split("\n"); //words = [abas, abased..]
// process the words and save them to a new file
if(cluster.isMaster) {
... perform some logic with words array
writeToFile(words);
var workers = [];
masterProcess();
} else {
childProcess();
}
function writeToFile(words) {
fs.unlinkSync('./filtered_words');
for (var index = 0; index < words.length; index++) {
var element = words[index];
fs.appendFile('./filtered_words',element + '\n', function (err) {
if (err) {
// append failed
} else {
// done
}
})
}
console.log('finished writing contents');
}
function masterProcess() {
// Fork workers
for (let i = 0; i < numCPUs; i++) {
const worker = cluster.fork();
workers.push(worker);
// Listen for messages from worker
worker.on('message', function(message) {
console.log(`Master ${process.pid} receives message '${JSON.stringify(message)}' from worker ${worker.process.pid}`);
if(message != "") {
for (var id in cluster.workers) {
console.log('worker killed');
cluster.workers[id].kill();
}
}
});
}
// Send message to the workers
var position = 0;
workers.forEach(function(worker) {
console.log(`Master ${process.pid} sends message to worker ${worker.process.pid}...`);
//worker.send({ msg: `Message from master ${process.pid}` });
worker.send(position);
position += 199;
}, this);
}
function childProcess() {
console.log(`Worker ${process.pid} started`);
var childResult = "";
process.on('message', function(message) {
console.log(`Worker ${process.pid} receives message '${JSON.stringify(message)}'`);
var words2 = fs.readFileSync("./filtered_words").toString().split("\n"); //words = [abas, abased..]
console.log(words2);
childResult = doLogic(words2,message);
});
}
function doLogic(words,position) {
...
}
This is what gets outputted.
Master 3700 is running
Forking process number 0...
Forking process number 1...
Forking process number 2...
Forking process number 3...
Worker 20580 started
Forking process number 4...
Forking process number 5...
Worker 1136 started
Forking process number 6...
Worker 8800 started
Forking process number 7...
Worker 11760 started
Master 3700 sends message to worker 20580...
Master 3700 sends message to worker 1136...
Master 3700 sends message to worker 8800...
Master 3700 sends message to worker 11760...
Master 3700 sends message to worker 26736...
Master 3700 sends message to worker 4568...
Master 3700 sends message to worker 8200...
Master 3700 sends message to worker 24892...
Worker 20580 receives message '0'
Worker 1136 receives message '199'
Worker 8800 receives message '398'
Worker 11760 receives message '597'
[ '' ]
[ '' ]
[ '' ]
[ '' ]
started with 1 number of words at position 199 ,looking for anagram 23170acc097c24edb98fc5488ab033fe
started with 1 number of words at position 0 ,looking for anagram 23170acc097c24edb98fc5488ab033fe
started with 1 number of words at position 398 ,looking for anagram 23170acc097c24edb98fc5488ab033fe
Worker 20580 LOOP1 : 0
started with 1 number of words at position 597 ,looking for anagram 23170acc097c24edb98fc5488ab033fe
Worker 26736 started
Worker 26736 receives message '796'
[ 'a',
'a',
'ails',
'ail',
'ainu',
'air',
'airs',
'al',
'al',
'ali',
'alison',
'alison',
'alit',
'airy',
'alot',
'alots',
'alow',
'alows',
'aloy',
'aloys',
'alpo',
'alps',
'also',
'alsop',
... I won't dump everything, but you can see at the end, the 5th worker picks up the data from textfile, so basically,when I create 8 workers, the first 4 of them somehow read nothing from the file and the other 4 will have no problem reading it.
Just so its clear, I am saying the workers read nothing, when output shows ['']