简体   繁体   English

Pipe 在刷新时生成进程标准输出到 function

[英]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.

相关问题 读取正在等待输入的生成进程的stdout - Read stdout of spawned process that is waiting for input stdout在向生成的进程发送多个请求时进行备份 - stdout backing up when sending multiple requests to spawned process 生成进程的标准输出在通过管道传输时工作异常 - stdout of spawned process works strange when being piped Node JS 如何在生成的子进程(powershell 脚本)中调用 function? - Node JS how to call a function in a spawned child process (powershell script)? 有没有办法从 child_process.execFile 生成的 python 脚本中获取“实时”output 行,而无需每次都刷新标准输出? - Is there a way to get 'live' output lines from a python script spawned by child_process.execFile without flushing stdout every time? 通过nodejs重新附加到生成的进程 - Reattaching to spawned process via nodejs 子进程 spawn 在 Electron 中产生 TypeError “stdout.on is not a function” - Child process spawn is producing TypeError “stdout.on is not a function” in Electron process.stdout在childProcess中没有函数“clearLine” - process.stdout does't have function “clearLine” in childProcess nodejs-csv-write-stream在标准输出的回调中给我'管道不是函数' - nodejs - csv-write-stream gives me 'pipe is not a function' inside a callback to stdout 如何在 node.js 中实时生成 childProcess.stdout - How to get realtime spawned childProcess.stdout in node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM