代码之家  ›  专栏  ›  技术社区  ›  Ionică Bizău

如何将输出分叉/集群进程管道到主stdout/stderr

  •  1
  • Ionică Bizău  · 技术社区  · 6 年前

    做的时候 worker = cluster.fork() 我希望能够将stdout和stderr流转发到master node.js进程。

    我该怎么做?

    我试着做:

    worker = cluster.fork();
    worker.process.stdout.pipe(process.stdout)
    //             ^ this is null
    

    解决方法是使用消息,但我希望能够流式传输内容。

    worker.on("message", function(msg){
      console.log("Master says:" + msg);
    });
    ...
    worker.send({message:'hello'});
    

    如何访问分叉进程/群集的STDUT?

    1 回复  |  直到 6 年前
        1
  •  -1
  •   sinbar    6 年前

    请看下面的代码

    对于主过程:

    const cluster = require('cluster');
    cluster.settings.stdio=[0, 1, 2, 'ipc'];
    cluster.settings.silent = true; //Whether or not to send output to parent's stdio. Default: false. 
    cluster.settings.exec = './hello' //the forked process's file path
    const childW = cluster.fork();
    const child = childW.process;
    

    分叉工艺: ./hello.js

    console.log('hello')