Nodejs always cann't capture child process's stdout data completely, unless child process fllush(stdout)

牧云@^-^@ 提交于 2019-12-20 03:02:49

问题


I use nodejs to captured its child process's stdout data, but always captured the former part of child process's stdout data. When I add fllush(stdout),It works OK. But I don't know why, and don't want to add flush(stdout).

Here is my code:

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");

Need your help! Thank you very much!


回答1:


By default, stdout in general is buffered until a newline is written. However, if stdout is not a tty (which is the case here with child_process.spawn()), all output is buffered, regardless of newlines.

If you don't want to use fflush() manually, you can disable stdout buffering entirely by doing setbuf(stdout, NULL); once at the beginning of your C program.



来源:https://stackoverflow.com/questions/29281500/nodejs-always-cannt-capture-child-processs-stdout-data-completely-unless-chil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!