【发布时间】:2014-06-06 03:51:06
【问题描述】:
我正在使用 Node.js v0.10.26,并尝试使用新的 stream2 apihttp://blog.nodejs.org/2012/12/20/streams2/, 特别是 Readable api,http://nodejs.org/api/stream.html#stream_class_stream_readable
但“结束”事件永远不会被调用,我不知道为什么。我认为这就是为什么我的下游转换流组件没有调用他们的 _flush、finish、close 事件,
var rqt = fs.createReadStream('some.csv.gz');
//readable
rqt.on('readable', function () { console.log('readable!'); })
.on('data', function (data) { console.log('Data!', data); })
.on('error', function (err) { console.error('Error', err);/* READ if there was an error receiving data. */ })
.on('end', function () { console.log('All done!'); /* READ fires when no more data will be provided. */ })
.on('finish', function () { console.log('All Finished!'); /*WRITEABLE */ })
.on('close', function () { console.log('close!'); /*WRITEABLE not all streams emit this*/ })
输出:
readable
readable
Data
readable
readable
Data
readable
更新 这样做的真正目标是做一个这样的流:
rqt.pipe(zlib.createGunzip()).pipe(csvp).on('finish',function(line){
console.log("Finished Parsing " + csvp.records + ' ' + line);
});
完全不使用经典模式, 在功能上加载压缩的 csv,解析 csv 并显示找到了多少。加载、解压缩和解析一切正常。
但是,以下都没有被调用: - csvp 的 _flush 永远不会被调用 - on('finish' 事件
【问题讨论】:
-
我也有同样的问题,请问您解决了吗?接受的答案似乎没有帮助
-
流就像一根穿过管道的消防水带,如果你处于流动模式,如果你有足够的输入,你必须把它排干..否则它会被备份并且永远不会结束。这是为了防止内存泄漏。
标签: javascript node.js stream fs