how to send message to parent process

前端 未结 1 907
感动是毒
感动是毒 2020-12-29 06:12

Can I send message to parent process?

master

var child =child_process.fork();

child.send({msg:msg})

child process

         


        
相关标签:
1条回答
  • 2020-12-29 06:55

    In short use: process.send()

    Longer example, I wrote awhile ago named forktest.js:

    var cp = require('child_process');
    
    if (!process.send) {
      var p = cp.fork(__dirname + '/forktest');
      p.send({
        count: 10
      });
      p.on('message', function(data) {
        process.exit(0);
      });
    } else {
      process.on('message', function(data) {
        console.log(data);
        data.count--;
        if (data.count === 0) {
          process.send({});
          process.exit(0);
        }
        var p = cp.fork(__dirname + '/forktest');
        p.send(data);
        p.on('message', function(data) {
          process.send(data);
          process.exit(0);
        });
      });
    }
    
    0 讨论(0)
提交回复
热议问题