简体   繁体   English

Node.JS——需要帮助理解原生流

[英]Node.JS -- need help understanding native streams

I'm trying to understand how the native implementation of streams works.我试图了解流的本机实现是如何工作的。 Here's the code:这是代码:

 const Stream = require('stream'); // define a custom class to read my data into the stream class SourceWrapper extends Stream.Readable { constructor(opt, content) { super(opt); this.content = content; this.len = content.length; this.index = 0; } _read() { let i = this.index++; if (i >= this.len) this.push(null); else { this.push(this.content[i]); } } } // generate some data const arr = (new Array(10000000)).fill(1); // declare the streams const firstStream = new SourceWrapper({objectMode: true}, arr); const transform = (x, enc, next) => next(undefined, x * Math.random(x, 10)); const firstMapStream = new Stream.Transform({objectMode: true}); firstMapStream._transform = transform; const secondMapStream = new Stream.Transform({objectMode: true}); secondMapStream._transform = transform; // create a promise to measure execution time const start = new Date(); new Promise((resolve, reject) => { firstStream .pipe(firstMapStream) .pipe(secondMapStream) .on('finish', () => resolve(new Date())); }) .then((end) => console.log('execTime', end - start));

The problem is that it works on small data sets (ie [1,2,3,4] ), but seems to terminate shortly after being run on a large set.问题是它适用于小数据集(即[1,2,3,4] ),但似乎在大数据集上运行后不久就终止了。

What am I missing?我错过了什么? Has it something to do with the objectMode ?objectMode吗?

Appreciate any help.感谢任何帮助。

The reason is that someone should to read data from the streams with binding data event listener.原因是有人应该使用绑定data事件侦听器从流中读取数据。 I've rewrite your code to make it clear to understand the issue.我已经重写了你的代码,以清楚地理解这个问题。 Also i fixed wrong index counting which skipped zero index.我还修复了跳过零索引的错误索引计数。

'use strict';
const Stream = require('stream');

// define a custom class to read my data into the stream
class SourceWrapper extends Stream.Readable {
  constructor(opt, content) {
    super(opt);
    this.content = content;
    this.len = content.length;
    this.index = 0;
  }

  _read() {
    let i = this.index;
    if (i >= this.len) {
      this.push(null);
    } else {
      this.push(this.content[i]);
    }
    this.index++;
  }
}


const transform = (x, enc, next) => next(undefined, x * Math.random(x, 10));

const transform1 = new Stream.Transform({objectMode: true});
transform1._transform = transform;

const transform2 = new Stream.Transform({objectMode: true});
transform2._transform = transform;


const write = new Stream.Writable({
    objectMode: true,
    write(value, enc, next) {
        // Do something like writing...
        next();
    }
});


// generate some data 
const arr = (new Array(1000000)).fill(1);
const read = new SourceWrapper({objectMode: true}, arr);

new Promise((resolve, reject) => {
    read
    .pipe(transform1)
    .pipe(transform2)
    .pipe(write)
    .on('finish', () => {
        resolve();
    });
})
.then(() => {
    console.log('Done');
});

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

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