1

我有麻烦,理解,为什么我创建的一半工作人员能够读取文件的内容,而其中一半人什么也没读。

这是我的代码:

// 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) {
        ...
    }

这就是输出的内容。

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',

...我不会转储所有内容,但最后你可以看到,第 5 个工作人员从文本文件中获取数据,所以基本上,当我创建 8 个工作人员时,前 4 个工作人员不知何故从文件中读取任何内容,而其他4个阅读它没有问题。

很清楚,当输出显示 [''] 时,我是说工人什么也没读

4

0 回答 0