Nodejs始终无法完全捕获子进程的stdout数据,除非子进程fllush(stdout) [英] Nodejs always cann't capture child process's stdout data completely, unless child process fllush(stdout)

查看:387
本文介绍了Nodejs始终无法完全捕获子进程的stdout数据,除非子进程fllush(stdout)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nodejs捕获其子进程的stdout数据,但始终捕获子进程的stdout数据的前一部分。当我添加 fllush(stdout)时,它可以正常工作。但是我不知道为什么,也不想添加flush(stdout)。

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).

这是我的代码:

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

child_process.c

printf("data\n");

需要您的帮助!

推荐答案

默认情况下, stdout 通常是缓冲直到写入换行符。但是,如果 stdout 不是tty(在 child_process.spawn()中就是这种情况),则所有输出

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.

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

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.

这篇关于Nodejs始终无法完全捕获子进程的stdout数据,除非子进程fllush(stdout)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆