簡體   English   中英

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

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

我試圖了解流的本機實現是如何工作的。 這是代碼:

 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));

問題是它適用於小數據集(即[1,2,3,4] ),但似乎在大數據集上運行后不久就終止了。

我錯過了什么? objectMode嗎?

感謝任何幫助。

原因是有人應該使用綁定data事件偵聽器從流中讀取數據。 我已經重寫了你的代碼,以清楚地理解這個問題。 我還修復了跳過零索引的錯誤索引計數。

'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