[英]Pipe spawned process stdout to function on flush
My goal is to spawn another binary file in a child process, then handle the stdout on a line-by-line basis (And do some processing against that line).我的目标是在子进程中生成另一个二进制文件,然后逐行处理标准输出(并对该行进行一些处理)。 For testing I'm using Node.
为了进行测试,我正在使用 Node。 To do this I tried a readable & writable stream but it a type error is throwed saying "The argument 'stdio' is invalid. Received Writable"
为此,我尝试了一个可读和可写的 stream 但它抛出了一个类型错误,说“参数'stdio'无效。收到可写”
const rs = new stream.Readable();
const ws = new stream.Writable();
const child = cp.spawn("node", [], {
stdio: [process.stdin, ws, process.stderr]
})
let count = 0;
ws.on("data", (data) => {
console.log(data, count)
});
Anyone have any ideas?有人有主意吗?
One way is to use the stdio streams returned by spawn
and manually pipe them:一种方法是使用
spawn
返回的 stdio 流并手动 pipe 它们:
const child = cp.spawn("node", [])
child.stdin.pipe(process.stdin)
child.stdout.pipe(ws)
child.stderr.pipe(process.stderr)
let count = 0;
ws.on("data", (data) => {
console.log(data, count)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.