dor*_*emi 5 javascript streaming stream node.js
我们有一些数据库调用可以轻松地传输10万条记录。我们遇到的问题是,每当我们使用这些流之一时,它就会挂住CPU,并似乎阻塞了所有其他进程。
我尝试了几种不同的技巧来减轻这种情况,但是现在有点卡住了。这是我最近一次尝试将流传输到使用它的Transform的尝试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);
Run Code Online (Sandbox Code Playgroud)
我注意到这个 Node.js问题可能相关,也可能不相关。
有人对如何解决这个问题有其他想法吗?
当您使用objectMode: true本机流实现时,可能必须缓冲和序列化数据。
因此,使用节流器流的想法是一个很好的想法,所以也许这个流会使用objectMode: false下游,您可以使用方便的objectMode: true.
请注意,混合不同类型的流可能会给您带来一些错误。
{objectMode: false} ==> {objectMode: true}可以(缓冲区只是另一种对象),
{objectMode: true} ==> {objectMode: false}不行(除非数据本身就是缓冲区)
| 归档时间: |
|
| 查看次数: |
810 次 |
| 最近记录: |