Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between "inherit" and "process pipe child"

Tags:

node.js

I spawn a shell to read from input then execute

const { spawn } = require('child_process')
const child = spawn('while :;do read a;$a;done', [], { shell: true,stdio:'inherit' })
child.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`)
})

with stdio:'inherit', it will consume 100% cpu. But if I remove stdio:'inherit' and add process.stdin.pipe(child.stdin), it works perfectly

the doc says

'inherit' - equivalent to [process.stdin, process.stdout, process.stderr]

So what's the actual meaning of inherit and the difference between them

like image 242
bilabila Avatar asked Apr 26 '18 14:04

bilabila


1 Answers

Little late to answer sorry, but for anybody else who is looking... I was confused about this too at first and the documentation is a bit light.

inherit means the stdios (stdin, stdout, stderr) for the parent process will be handed off to the child process. So if the stdin on child process is set to inherit, any key presses will target the child process and not the parent.

pipe means that the stdios for both the parent and child are mapped together. However accessing piped data through those pips is accessed through events. So if you spawn a child process with a stdout of pipe, you can access any output of that process through the use of childProcess.stdout.on('data', callbakFn)

like image 183
McShaman Avatar answered Oct 16 '22 18:10

McShaman