我最近参加了一个node.js聚会,演讲的人说了一些类似的话:
“你需要生成器才能进行多轮并行io。回调不会影响它。”
我是node.js的新手,不知道这是什么意思。有人能解释一下吗?
编辑:根据评论中的要求,这里有一个更详细的版本:我参加了一个关于node.js的介绍会议。听众中有人问演讲者,他认为node.js最大的缺点是什么。他说,在node.js获得生成器之前,它没有一个很好的解决方案来处理多轮并行I/O。任何大规模的web应用都必须这样做。在多轮中并行命中memcache是一个例子,数据库和第三方API是其他例子。任何来自Python、Ruby或Go等支持生成器、线程或微线程的语言的人都很难接受平台可以完全依赖回调的观点。
发布于 2013-06-10 02:08:22
他可能指的是序列帮助器,在下一个任务的回调中运行一个任务将意味着链将同步运行。
这是一个我用来向Mongo集合中插入大量数据的生成器示例。这将生成并行执行的操作序列。在这种情况下,通过使用回调方法链接来执行一百万次插入是不太实际的。这就是为什么我使用下面这样的生成器/序列帮助器。
var Inserter = function (collection) {
this.collection = collection;
this.data = [];
this.maxThreads = 30;
this.currentThreads = 0;
this.batchSize = 1000;
this.queue = 0;
this.inserted = 0;
this.startTime = Date.now();
};
Inserter.prototype.add = function(data) {
this.data.push(data);
};
Inserter.prototype.insert = function(force) {
var that = this;
if (this.data.length >= this.batchSize || force) {
if (this.currentThreads >= this.maxThreads) {
this.queue++;
return;
}
this.currentThreads++;
console.log('Threads: ' + this.currentThreads);
this.collection.insert(this.data.splice(0, this.batchSize), {safe:true}, function() {
that.inserted += that.batchSize;
var currentTime = Date.now();
var workTime = Math.round((currentTime - that.startTime) / 1000)
console.log('Speed: ' + that.inserted / workTime + ' per sec');
that.currentThreads--;
if (that.queue > 0) {
that.queue--;
that.insert();
}
});
}
};https://stackoverflow.com/questions/17012134
复制相似问题