简体   繁体   English

节点读取流:什么时候进行流传输?

[英]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. 创建流时,它会暂停,因此无法dataend事件。 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.

相关问题 节点双工流在读取时实际上不会发出数据 - Node duplex stream doesn't actually emit data when read 在一个项目中同时使用Node js和react时,会发生服务端渲染吗? 我是新手 - When Node js and react are used in a project at the same time, does server side rendering happen? I'm new to this React.js - 未捕获的类型错误:无法读取未定义的属性(硬编码数据时不会发生) - React.js - Uncaught TypeError: Cannot read properties of undefined (does not happen when data is hardcoded) Node JS 关闭读取流 - Node JS close a read stream 为什么在使用 Node.js 调试器时某些变量显示为“未定义”? 正常运行程序时不会发生这种情况 - Why do some variables show up as "undefined" when using the Node.js debugger? This does not happen when running the program normally 传输到中间 stream 时,视频没有流式传输 - video is not streaming when piped to a middle stream 滚动事件未发生时的运行功能 - Run Function When a Scroll Event Does NOT Happen 为什么在我运行代码时会发生这种情况? - Why does this happen when I run the code? Angular2:属性绑定何时发生? - Angular2: when does property binding happen? 何时在DOM环境中进行重排? - When does reflow happen in a DOM environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM