Node.js写入子进程
我正在尝试使用child_process
模块和覆盆子pi上的mpg123播放器在node.js中创建一个mp3 webradio。
这是问题:当我尝试写入流时,我总是得到一个错误:
Error: write EPIPE
at exports._errnoException (util.js:1026:11)
at WriteWrap.afterWrite (net.js:795:14)
这是我试图测试的:
//create a subprocess
var test = childProcess.exec('ls /home/pi/music');
//pipe all the output to the process output
test.stdout.pipe(process.stdout);
test.stderr.pipe(process.stderr);
test.stdin.setEncoding('utf-8');
//here I just want to write a key to the subprocess
test.stdin.write('q');
test.stdin.end()
任何人都知道,该怎么做,创建的写入流将不会被关闭,直到一切都完成?
当然,我仍然在谷歌搜索这个:
Child_process throw an Error: write EPIPE
.html#child_process_subprocess_stdin
.html#stream_writable_write_chunk_encoding_callback
请帮忙!
回答如下:EPIPE
写入错误意味着有人正在写入一个没人会读的管道。
此外,因为在没有人处理它的上下文中引发此错误,所以错误对于您的进程来说是致命的。遗憾的是,node并没有很好地识别哪个流,但是我们可以在你的代码中猜测它,而且这个代码只写了一个流......
如果添加错误处理程序,则可以验证错误是来自“stdin”流:
test.stdin.on('error', (...args) => { console.log('stdin err', args); });
现在错误将被“处理”(糟糕),并且该过程将继续,因此您至少知道它的来源。
特别是这个test.stdin.write('q')
正在写一个q
到你的过程的stdin。但你的孩子过程是ls
。它在stdin上不接受任何东西,所以它关闭stdin。因此,当父尝试写入现在关闭的管道时,OS会发回EPIPE错误。所以,停止这样做。
您可能期望ls
的行为与您交互式键入ls
时的行为相同,但它可能不会(我怀疑您的交互式ls
正在通过寻呼机,并且您期望您需要退出该寻呼机?)此子进程是更像是以交互方式运行/bin/ls
。
Node.js写入子进程
我正在尝试使用child_process
模块和覆盆子pi上的mpg123播放器在node.js中创建一个mp3 webradio。
这是问题:当我尝试写入流时,我总是得到一个错误:
Error: write EPIPE
at exports._errnoException (util.js:1026:11)
at WriteWrap.afterWrite (net.js:795:14)
这是我试图测试的:
//create a subprocess
var test = childProcess.exec('ls /home/pi/music');
//pipe all the output to the process output
test.stdout.pipe(process.stdout);
test.stderr.pipe(process.stderr);
test.stdin.setEncoding('utf-8');
//here I just want to write a key to the subprocess
test.stdin.write('q');
test.stdin.end()
任何人都知道,该怎么做,创建的写入流将不会被关闭,直到一切都完成?
当然,我仍然在谷歌搜索这个:
Child_process throw an Error: write EPIPE
.html#child_process_subprocess_stdin
.html#stream_writable_write_chunk_encoding_callback
请帮忙!
回答如下:EPIPE
写入错误意味着有人正在写入一个没人会读的管道。
此外,因为在没有人处理它的上下文中引发此错误,所以错误对于您的进程来说是致命的。遗憾的是,node并没有很好地识别哪个流,但是我们可以在你的代码中猜测它,而且这个代码只写了一个流......
如果添加错误处理程序,则可以验证错误是来自“stdin”流:
test.stdin.on('error', (...args) => { console.log('stdin err', args); });
现在错误将被“处理”(糟糕),并且该过程将继续,因此您至少知道它的来源。
特别是这个test.stdin.write('q')
正在写一个q
到你的过程的stdin。但你的孩子过程是ls
。它在stdin上不接受任何东西,所以它关闭stdin。因此,当父尝试写入现在关闭的管道时,OS会发回EPIPE错误。所以,停止这样做。
您可能期望ls
的行为与您交互式键入ls
时的行为相同,但它可能不会(我怀疑您的交互式ls
正在通过寻呼机,并且您期望您需要退出该寻呼机?)此子进程是更像是以交互方式运行/bin/ls
。