节点读取stream:stream何时发生?

这是一个代码示例,它与您可以从networking或文档中获得的代码没有多大区别:

var fs = require('fs'); var r = fs.createReadStream(process.argv[2], { encoding: 'utf8' }); r.on('data', function (chunk) { console.log("chunk: >>>" + chunk + "<<<"); }); r.on('end', function () { console.log("This is the end"); }); 

什么使我困惑:什么时候触发事件的stream发生? 显然不是直接在读取stream的构造上,因为那么在我们到达之前就完成了,并且事件监听代码将永远不会被执行(这是完美的)。

什么让我担心:如果来电太迟,是否有理由有机会错过一个事件?

答案是否定的,在节点0.10.x和更高版本中是不可能的。 当stream创build时,它被暂停,所以dataend事件都不能被发射。 当您添加data侦听器(而不是end侦听器)时,该stream将自动恢复。

另外值得一提的是,在当前的“tick”结束之前不会出现IO,所以如果在同一个tick中附加data监听器总是安全的,即使对于较早的节点版本也是如此。 例如:

 stream.resume(); stream.on('data', ...); // <- same tick, same javascript invocation = safe stream.resume(); setImmediate(function () { stream.on('data', ...); // <- different tick, different javascript invocation = unsafe }); 

这可能听起来令人困惑,但在process.nextTickcallback中添加监听器也是安全的,因为它实际上是在CURRENT之前在任何IOcallback之前调用(这是一个非常糟糕的命名)。

最简单的方法是,所有你提供的代码都是阻塞的,因此在stream上没有任何活动可以发生,直到vkurchatkin解释为止。 只有在当前滴答的JavaScript执行完成后,streamIO才能开始。

 var fs = require('fs'); var r = fs.createReadStream(process.argv[2], { encoding: 'utf8' }); // Stream created in paused state, JS code is blocking IO // We're still in the current tick so no IO could have occured since the above lines r.on('data', function (chunk) { console.log("chunk: >>>" + chunk + "<<<"); }); // We're still in the current tick so no IO could have occured since the above lines r.on('end', function () { console.log("This is the end"); }); // We've left the current tick so the internal IO code will now execute and call the bound events if necessary