且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Node.js写入子进程

更新时间:2022-06-22 04:24:04

EPIPE 写入错误表示某人正在写入管道,没人会读取.

The EPIPE write error means someone is writing into a pipe that no one will ever read from.

此外,由于此错误是在没有人处理的上下文中引发的,因此该错误对于您的过程是致命的.可悲的是,node并不能很好地识别哪个流,但是我们可以猜测它在您的代码中,并且此代码仅写入了一个流...

Additionally, because this error is being raised in a context where no one handles it, the error is fatal for your process. Sadly, node doesn't do a good job of identifying which stream, but we can guess that its in your code, and there is only one stream being written to by this code ...

如果添加错误处理程序,则可以验证错误是否来自"stdin"流:test.stdin.on('错误',(... args)=> {console.log('stdin err',args);});

You can verify that the error is coming from the "stdin" stream if you add an error handler: test.stdin.on('error', (...args) => { console.log('stdin err', args); });

现在,错误(较差)将被处理",并且该过程将继续,因此您至少知道错误的出处.

Now the error will be "handled" (poorly), and the process will continue, so you at least know where its coming from.

具体来说,此 test.stdin.write('q')正在将 q 写入流程的标准输入.但是您的子进程是 ls .它在stdin上不接受任何内容,因此关闭了stdin.因此,当父级尝试写入现在关闭的管道时,操作系统会发回EPIPE错误.因此,请停止这样做.

Specifically this test.stdin.write('q') is writing a q to the stdin of your process. But your child process is ls. It doesn't accept anything on stdin, so it closes stdin. So the OS sends an EPIPE error back when the parent tries to write to the now-closed pipe. So, stop doing that.

您可能希望 ls 与以交互方式键入 ls 时的行为相同,但可能不会(我怀疑您的交互式 ls 正在通过一个寻呼机,并且您期望要退出该寻呼机?)此子进程更像是交互式运行/bin/ls .

You probably expect ls to behave the same as when you type ls interactively, but it probably does not (I suspect your interactive ls is going through a pager, and you're expecting that you need to quit that pager?) This child process is more equivalent to running /bin/ls interactively.