I'm using NodeJS with PhantomJS. My goal is to create 4x node instances with node cluster, each with 2 phantom children. And my code looks like this:
cluster.js:
var numCPUs = 4;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
});
} else {
require("./app");
}
App.js looks like this:
var instances = [];
var phantom = require('phantom');
function InstanceManager(instCount) {
for (var i = 0; i < instCount; i++) {
phantom.create(function(phantomInstance) {
instances.push({
cycle: 0,
locked: false,
instance: phantomInstance
});
});
}
}
InstanceManager(2);
setInterval(function() {
var i = 0;
console.log('--' + instances.length);
}, 5000);
So after running cluster.js
the expected output in node console each 5 seconds should be:
--2
--2
--2
--2
but instead looks like this:
--0
--0
--0
--8
Why the phantom instances aren't attached to the proper worker?