异步执行的NodeJS秩序 [英] async nodejs execution order

查看:114
本文介绍了异步执行的NodeJS秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不processItem开始执行。是否只要某些项目会被压入队列开始?或者必须在队列上的第一个项目之前完成循环开始执行?

  VAR processItem =函数(项目,回调){
    的console.log(项目)
    回电话();
}
变种myQueue中= async.queue(processItem,2)
对于(指数= 0;指数 - LT; 1000; ++指数){
    myQueue.push(指数)
}


解决方案

简单的答案是,所有的任务将被添加到队列,然后在随机(不确定)的顺序执行。


背景资料,通过 https://github.com/caolan/async#queueworker-concurrency

队列(工人,并发)

创建一个队列与指定的并发对象。添加到队列任务处理并行(达并发限制)。如果所有的工人是在进步,任务排队,直到有可用。一旦一个工人完成了任务,即的任务 回调被调用。


在换句话说,顺序为未定义。如果您需要的任务按照特定的顺序来执行,你应该使用不同的异步原始的,如系列(任务,[回调])
https://github.com/caolan/async#seriestasks-callback

When does processItem start executing. Does it start as soon as some items are pushed onto the queue? Or must the for loop finish before the first item on the queue starts executing?

var processItem = function (item, callback) {
    console.log(item)
    callback();
}


var myQueue = async.queue(processItem, 2)


for (index = 0; index < 1000; ++index) {
    myQueue.push(index)
}

解决方案

The simple answer is that all of your tasks will be added to the queue, and then executed in a random (undefined) order.


background information, via https://github.com/caolan/async#queueworker-concurrency

queue(worker, concurrency)

Creates a queue object with the specified concurrency. Tasks added to the queue are processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one becomes available. Once a worker completes a task, that task's callback is called.


In other words, the order is undefined. If you require the tasks to be executed in a particular order, you should be using a different asynchronous primitive, such as series(tasks, [callback]) https://github.com/caolan/async#seriestasks-callback

这篇关于异步执行的NodeJS秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆