[英]Node read stream: when does the streaming happen?
Here is a code example that is not much different from what you can get off the net or documentation: 这是一个代码示例,与您可以从网上获得的文档或文档没有太大不同:
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");
});
What puzzles me: when does the streaming that triggers events happen? 令我感到困惑的是:什么时候触发事件的流媒体发生? Apparently not directly on construction of the read stream, because then it would be done before we get to the
on
s, and the event-listening code would never be executed (which it is, this works perfectly). 显然,不是直接在读取流的构造上进行的,因为这将在到达
on
s之前完成,并且事件监听代码将永远不会执行(这是完美的)。
What worries me: is there a theoretical chance that an event is missed if the on
call comes too late? 我担心的:有一个理论的机会,如果事件被错过
on
来电时为时已晚?
The answer is no, it's not possible in node 0.10.x and later. 答案是否定的,在节点0.10.x和更高版本中是不可能的。 When stream is created, it is paused, so neither
data
nor end
events can't be emitted. 创建流时,它会暂停,因此无法
data
或end
事件。 When you add data
listener (but not end
listener), the stream is automatically resumed. 添加
data
侦听器(但不添加end
侦听器)时,流将自动恢复。
Also worth mentioning that no IO can occur before current "tick" ends, so if you attaching data
listeners in the same tick is always safe, even for earlier node versions. 还值得一提的是,在当前“滴答”结束之前不会发生任何IO,因此即使在同一滴答中附加
data
侦听器也总是安全的,即使对于较早的节点版本也是如此。 For example: 例如:
stream.resume();
stream.on('data', ...); // <- same tick, same javascript invocation = safe
stream.resume();
setImmediate(function () {
stream.on('data', ...); // <- different tick, different javascript invocation = unsafe
});
It might sound confusing, but adding listener in process.nextTick
callback is also safe because it's actually called right after CURRENT tick before any IO callback (a case of really bad naming). 这听起来可能令人困惑,但是在
process.nextTick
回调中添加侦听器也是安全的,因为它实际上是在CURRENT滴答之后立即在任何IO回调之前进行调用(这种情况确实很糟糕)。
The easiest way to think of this is that all the code you've provided is blocking therefore no activity can occur on the stream until the current tick ends as vkurchatkin explains. 想到这一点的最简单方法是,您提供的所有代码都被阻止,因此,如vkurchatkin所解释的那样,直到当前滴答结束为止,流上才能发生任何活动。 Only when the JavaScript execution for the current tick finishes can the stream IO begin.
只有当当前滴答声的JavaScript执行完成时,流IO才能开始。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.