最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Node.js写入子进程

IT培训 admin 3浏览 0评论

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论