【问题标题】:How to pipe the output forked/cluster processes into the master stdout/stderr如何将输出分叉/集群进程通过管道传输到主 stdout/stderr
【发布时间】:2019-03-21 14:42:40
【问题描述】:

在执行worker = cluster.fork() 时,我希望能够将 stdout 和 stderr 流转发到主 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'});

如何访问分叉进程/集群的标准输出?

【问题讨论】:

    标签: javascript node.js process stdout stderr


    【解决方案1】:

    请看下面的代码

    对于主进程:

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

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 2011-05-03
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多