在执行 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'});
<小时/>
如何访问 fork 进程/集群的标准输出?
最佳答案
请看下面的代码
对于主进程:
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;
对于 fork 进程:./hello.js
console.log('hello')
关于javascript - 如何将输出 fork /集群进程通过管道传输到主 stdout/stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846069/