做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'});
如何访问派生的进程/集群的标准输出?
答案 0 :(得分:-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')