开发者

node.js looping on a spawn call

I am trying to start a long running program that gives lots of output to sdtout. I was trying to use the method below to read data on intervals.

s    = spawn('blah_long_running_expect.pl --device server');
setInterval( function (){   
  s.stdout.on('data', function (data) {
     console.log("he开发者_Python百科rehere");})
},1000);

}

This is part of a http server instance...there is a createserver somewhere before that. My eventual idea is to use response.write to output to screen

I'm getting the below error right now. I'm wondering if this setinterval way of reading lines is completely wrong?

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. 
Use emitter.setMaxListeners() to increase limit.
Trace: 
    at Socket.<anonymous> (events.js:126:17)
    at Timer.callback (/tmp/requestHandlers.js:12:17)


s.stdout.on('data', function (data) { console.log("herehere");}) },1000);

The .on call adds an event listener. What you want to do instead is something like this.

s = spawn('blah_long_running_expect.pl --device server');
var dd = "";
s.stdout.on('data', function(d) {
  console.log('some data : ' + d);
  dd += d;
});
s.stdout.on('end', function() {
  console.log('end');
});
setInterval( function (){   
  console.log(dd);
  dd = "";
},1000);


With your code, it is adding a new event listener every second. You don't need to worry about splitting the output into chunks; Node.js takes care of that, and that is exactly what 'data' event is for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜