Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js-多轮并行io_Javascript_Node.js_Io - Fatal编程技术网

Javascript Node.js-多轮并行io

Javascript Node.js-多轮并行io,javascript,node.js,io,Javascript,Node.js,Io,我最近在一个node.js会议上,演讲的人说了一些类似的话: “您需要生成器才能执行多轮并行io。回调无法解决此问题” 我是node.js新手,不知道这意味着什么。有人能解释一下吗 编辑:如评论中所问,这里有一个更详细的版本:我参加了一个介绍node.js的会议。听众中有人问演讲者,他认为node.js最大的缺点是什么。他说,在node.js获得生成器之前,对于多轮并行I/O并没有一个好的解决方案。任何大型web应用程序都必须这样做。在多轮中并行命中memcache就是一个例子,数据库和第三方a

我最近在一个node.js会议上,演讲的人说了一些类似的话:
“您需要生成器才能执行多轮并行io。回调无法解决此问题”

我是node.js新手,不知道这意味着什么。有人能解释一下吗


编辑:如评论中所问,这里有一个更详细的版本:我参加了一个介绍node.js的会议。听众中有人问演讲者,他认为node.js最大的缺点是什么。他说,在node.js获得生成器之前,对于多轮并行I/O并没有一个好的解决方案。任何大型web应用程序都必须这样做。在多轮中并行命中memcache就是一个例子,数据库和第三方api则是另一个例子。任何来自Python、Ruby或Go等支持生成器、线程或微线程的语言的人都很难接受平台完全依赖回调

他可能指的是序列助手,在下一个任务的回调中运行一个任务意味着链将同步运行

这是一个用于将大量数据插入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();
            }
        });
    }
};

我可能很傻,但这对我来说毫无意义(没有上下文)。他可能是指即将到来的js?这是什么样的node.js meetup。请用事实来支持你的问题,以便我们理解。我在报告中添加了更多细节question@tldr这是胡说八道。生成器(如Python中的生成器)只是助手。您也可以在JavaScript中使用类似的结构。生成器和回调之间绝对没有关系。大福?发电机与并行I/O有什么关系?你应该告诉他nginx是单线程的,但它在I/O方面没有任何问题。你应该忘记那个家伙。他不知道自己在说什么。看起来你本可以用;)