【发布时间】:2014-06-23 12:46:36
【问题描述】:
我们有一些数据库调用可以轻松流式传输 10 万条记录。我们遇到的问题是,每当我们使用这些流之一时,它都会与 CPU 挂钩,并且似乎会阻塞所有其他进程。
我尝试了一些不同的技巧来缓解这种情况,但现在有点卡住了。这是我最近尝试将流通过管道传输到使用 process.nextTick 的转换。
var stream = require('stream');
var util = require('util');
function StreamThrottler() {
stream.Transform.call(this, { objectMode: true });
}
util.inherits(StreamThrottler, stream.Transform);
StreamThrottler.prototype._transform = function(chunk, encoding, cb) {
process.nextTick(function() {
console.log('chunk');
// note: I'm intentionally not pushing the chunk
// onto the stream for testing
cb();
});
};
StreamThrottler.prototype._flush = function(cb) {
cb();
};
var streamThrottler = new StreamThrottler();
// now the db call
this.largeDatabaseResultStream().pipe(streamThrottler);
我注意到this Node.js 问题可能相关也可能不相关。
有人对如何解决这个问题有任何其他想法吗?
【问题讨论】:
-
包含您正在使用的数据库模块的名称可能会有所帮助。
标签: javascript node.js stream streaming