node.js child_process.spawn no stdout除非'inherit' [英] node.js child_process.spawn no stdout unless 'inherit'

查看:303
本文介绍了node.js child_process.spawn no stdout除非'inherit'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从节点中的 spawn ed child_process 捕获stdout .js (0.10.29)。

I'm trying to capture the stdout from a spawned child_process in node.js (0.10.29).

现在我只想用 ping

以下代码无法打印(但会ping)

The following code doesn't print (but does ping)

var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var util = require('util')

var ping = spawn('ping', ['127.0.0.1'], {stdio: 'pipe'});

ping.stdout.on('data', function(data){
    util.print(data);
})

ping.stderr.on('data', function(data){
    util.print(data);
})

如果我将 stdio:'pipe'更改为 stdio:'inherit'并且摆脱 stdout / stderr 这样的钩子:

If I change stdio: 'pipe' to stdio: 'inherit' and get rid of the stdout/stderr hooks like so:

var ping = spawn('ping', ['127.0.0.2'], {stdio: 'inherit'});

// ping.stdout.on('data', function(data){
//  util.print(data);
// })

// ping.stderr.on('data', function(data){
//  util.print(data);
// })

我得到

PING 127.0.0.2 (127.0.0.2): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1

如果我将地址从 127.0.0.2 更改为 127.0.0.1 ,我知道将响应ping并使用我得到的原始代码

If I change the address from 127.0.0.2 to 127.0.0.1, which I know will respond to the pings and use the original code I get

PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.060 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.152 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.124 ms

当ping不是<$ c $时,为什么stdout / stderr没有触发数据事件的任何想法c> ping 或继承

Any ideas why stdout/stderr are not firing data events when the ping isn't pinging or inheriting?

推荐答案

关于控制台打印,在分块和缓冲方面有很多问题修复和功能改进。由于问题不再具有可再现性,我认为这可能是由于当前Node的一个记录不足的行为,基于可用于打印的数据字节数。

There have been lot of issue fixes as well as feature improvements with respect to console printing, on the lines of chunking and buffering. As the issue is no more reproducible, I would assume that this could be due to one of the under-documented behavior of the then Node, based on how many bytes of data is available for printing.

由于这个问题仍然存在,我会要求发端人看看你是否对这个解释感到满意,或者正在寻找更具体的解释,我需要在节点的所述版本中重现这个问题。进一步调试。

As this question is still open, I would request originator to see if you are satisfied with this explanation, or are looking for more concrete ones, which case I would need to reproduce this in the said version of node and debug further.

这篇关于node.js child_process.spawn no stdout除非'inherit'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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