为什么在节点块child_process.exec中打开fifo管道?

时间:2016-10-27 21:10:19

标签: javascript node.js bash mkfifo

当我在bash中运行时:

mkfifo im-a-pipe && node -e '
var fs = require("fs")
var childProcess = require("child_process")

console.log("pre-open")
fs.open("im-a-pipe", "w", function(err, fd){
if(err)
throw err
console.log("opened")
})
console.log("post-open")
childProcess.exec("echo wat")
console.log("YOU CAN NOT SEE MEEE")
'

我期待以下输出:

pre-open
post-open
YOU CAN NOT SEE MEEE

但是节点在打印前两行之后会等待:

pre-open
post-open

我认为这可能与管道堵塞有关,直到某些东西打开另一侧,但这种行为让我感到惊讶。

我是否遗漏了这些功能应该如何运作?

2 个答案:

答案 0 :(得分:1)

这可能是您的操作系统中的错误,也可能只是旧版本的Node。适用于Mac的Node 4.我可以在Debian上运行它,但直到他升级到Node 8。

答案 1 :(得分:0)

这正是管道工作的方式(命名或匿名)。作者将阻止,直到另一端有一个读者,并且读者将在没有作者的情况下阻止。你可以说这是一个基本原则。

这是您可以在命令行上执行的一个小演示。您将需要两个终端会话,我将其称为A和B.

在A:

cat mypipe

会话A将阻止。现在进行会议B:

ls

应显示cat输出,并取消阻止会话A.

您也可以先执行# ERR: Lost connection to MySQL server during query (MySQL error code: 2013, SQLState: HY000 ) Segmentation fault (core dumped) Debug with gdb. Reading symbols from ./http_monitor...done. [New LWP 10130] [New LWP 10131] [New LWP 10132] [New LWP 10133] [New LWP 10134] [New LWP 10200] [New LWP 10125] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Core was generated by `./http_monitor'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000000000040715a in CMySQL::query (this=0x1b0d0c0, sql=0x7fb83c000b08 "SELECT file_id,filename,path,upload_time FROM bl_files WHERE upload_time > 0 AND upload_time < 1476966545;", info=0x42a062 "CleanFile notice") at CMySQL.cpp:296 (gdb) where #0 0x000000000040715a in CMySQL::query (this=0x1b0d0c0, sql=0x7fb83c000b08 "SELECT file_id,filename,path,upload_time FROM bl_files WHERE upload_time > 0 AND upload_time < 1476966545;", info=0x42a062 "CleanFile notice") at CMySQL.cpp:296 尝试此操作。