簡體   English   中英

Node.js寫入子進程

[英]Node.js write to child process

我正在嘗試使用child_process模塊和樹莓child_process的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()

有人知道該怎么辦,直到完成所有操作后,創建的寫入流才會關閉?

當然,我仍然在Google上搜索過此內容:

Child_process拋出錯誤:寫EPIPE

https://github.com/nodejs/node/issues/2985

https://nodejs.org/api/child_process.html#child_process_subprocess_stdin

https://nodejs.org/api/stream.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寫入進程的標准輸入。 但是您的子進程是ls 它在stdin上不接受任何內容,因此關閉了stdin。 因此,當父級嘗試寫入現在關閉的管道時,操作系統會發回EPIPE錯誤。 因此,停止這樣做。

您可能希望ls的行為與您以交互方式鍵入ls時的行為相同,但可能不會(我懷疑您的交互式ls正在通過傳呼機,並且您期望您需要退出該傳呼機?)此子進程是更相當於以交互方式運行/bin/ls

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM