Nodejs总是不能完全捕获subprocess的stdout数据,除非subprocessfllush(stdout)

我使用nodejs来捕获它的subprocess的stdout数据,但总是捕获subprocess的stdout数据的前一部分。 当我添加fllush(stdout)时 ,它工作正常。 但是我不知道为什么,也不想添加flush(stdout)。

这是我的代码:

var tail_child = spawn(exefile, [arg1, arg2, arg3]); tail_child.stdin.write('msg\n'); tail_child.stdout.on('data', function(data) { console.log(data); }); 

child_process.c

 printf("data\n"); 

需要你的帮助! 非常感谢你!

默认情况下,一般情况下stdout被caching,直到写入换行符。 但是,如果stdout不是一个tty(这是child_process.spawn()这里的情况),所有输出都被缓冲,不pipe换行符如何。

如果你不想手动使用fflush() ,你可以通过setbuf(stdout, NULL);完全禁用stdout缓冲setbuf(stdout, NULL); 一次在你的C程序开始。