简体   繁体   English

buffer 和 stream - 它们有什么关系?

[英]buffer and stream - how are they related?

I am putting some code here:我在这里放一些代码:

const { createReadStream, ReadStream } = require('fs');

var readStream = createReadStream('./data.txt');

readStream.on('data', chunk => {
  console.log('---------------------------------');
  console.log(chunk);
  console.log('---------------------------------');
});

readStream.on('open', () => {
  console.log('Stream opened...');
});

readStream.on('end', () => {
  console.log('Stream Closed...');
});

So, stream is the movement of data from one place to another.因此,stream 是数据从一个地方移动到另一个地方。 In this case, from data.txt file to my eyes since i have to read it.在这种情况下,从data.txt文件到我的眼睛,因为我必须阅读它。

I've read in google something like this:我在谷歌上读过这样的东西:

Typically, the movement of data is usually with the intention to process it, or read it, and make decisions based on it.通常,数据的移动通常是为了处理它或读取它,并根据它做出决策。 But there is a minimum and a maximum amount of data a process could take over time.但是随着时间的推移,进程可能会接收最少和最多的数据量。 So if the rate the data arrives is faster than the rate the process consumes the data, the excess data need to wait somewhere for its turn to be processed.因此,如果数据到达的速率快于进程消耗数据的速率,则多余的数据需要在某个地方等待轮到它处理。

On the other hand, if the process is consuming the data faster than it arrives, the few data that arrive earlier need to wait for a certain amount of data to arrive before being sent out for processing.另一方面,如果进程消耗数据的速度快于数据到达的速度,则较早到达的少数数据需要等待一定数量的数据到达才能被发送出去进行处理。

My question is: which line of code is "consuming the data, processing the data"?我的问题是:哪一行代码在“消费数据,处理数据”? is it console.log(chunk) ?console.log(chunk)吗? if I had a huge time consuming line of code instead of console.log(chunk) , how would my code not grab more data from buffer and wait until my processing is done?如果我有大量耗时的代码行而不是console.log(chunk) ,我的代码怎么会不从缓冲区获取更多数据并等待我的处理完成? in the above code, it seems like, it would still come into readStream.on('data')'s callback..在上面的代码中,它似乎仍然会进入readStream.on('data')'s回调..

My question is: which line of code is "consuming the data, processing the data"我的问题是:哪一行代码是“消费数据,处理数据”

The readStream.on('data', ...) event handler is the code that "consumes" or "processes" the data. readStream.on('data', ...)事件处理程序是“使用”或“处理”数据的代码。

if I had a huge time consuming line of code instead of console.log(chunk), how would my code not grab more data from buffer and wait until my processing is done?如果我有一大行耗时的代码而不是 console.log(chunk),我的代码怎么会不从缓冲区中获取更多数据并等待我的处理完成呢?

If the time consuming code is synchronous (eg blocking), then no more data events can happen until after your synchronous code is done because only your event handler is running (in the single-threaded event loop driven architecture of node.js).如果耗时代码是同步的(例如阻塞),那么在同步代码完成之前不会再发生data事件,因为只有您的事件处理程序在运行(在 node.js 的单线程事件循环驱动架构中)。 No more data events will be generated until you return control back from your event handler callback function.在您从事件处理程序回调 function 返回控制权之前,不会生成更多data事件。

If the time consuming code is asynchronous (eg non-blocking and thus has returned control back to the event loop), then more data events certainly can happen even though a prior data event handler has not entirely finished it's asynchronous work yet.如果耗时的代码是异步的(例如,非阻塞并因此将控制返回给事件循环),那么即使先前的data事件处理程序尚未完全完成其异步工作,也肯定会发生更多data事件。 It is sometimes appropriate to call readStream.pause() while doing asynchronous work to tell the readStream not to generate any more data events until you are ready for them and you can then readStream.resume() .有时在执行异步工作时调用readStream.pause()是合适的,以告诉 readStream 不要再生成任何data事件,直到您准备好它们,然后您可以readStream.resume()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM