node. JS Async Process Controller--queue (queue)

Source: Internet
Author: User

The queue process controller is a parallel process controller, but the difference between it and parallel is that the queue can control the execution of several functions at once, while the parallel simply allows all functions to execute in parallel.

Examples are as follows:

var q = async.queue (function  (OBJ,CB) {setTimeout (function  () {Console.log (  OBJ); CB ();},obj.time)},1) for (var i = 0; i<100; i++) {
  Console.log (1);
  
Q.push ({name:i,time:i*1000},function (err) {
Console.log (ERR);
})}; for  (var i = 0; i<100; i++) {console.log (2); Q.push ({name:1,time:1000}, function (err) {
Console.log (ERR);
})};

Here is a pseudo-asynchronous execution of the way, the queue has two parameters, the first is a function, the second is the number of processes executed, the first parameter contains two parameters, the first is the passed object, the second is a callback function, in the example only after Console.log (obj) executes the call CB ( Queue will return an identifier (q in the example) if it is executed down.

The Q.push in the example is to add data to the queue (which will be received by obj in queue), and the second parameter is a callback function that will trigger the callback if there is an exception. In the example, insert only one data at a time, or you can insert more than one such as Q.push ([{name:0,time:1000}],{name:1,time:2000}]);(here are the arrays).

Here, in order to verify that the loop is actually executing, two numbers are printed in the loop. The result of the execution is to cycle through the print Console.log (1) and then cycle through the Console.log (2), which shows that two loops are executed, The first loop is then added to the queue object, and then the second loop is printed out, fully comply with the nature of the queue: FIFO, after the last rule, not because of the execution of time and make the results confusing. If you change the number of jobs in the queue to 2, you will print two each time.

The queue also has several functions, such as:

When the number of worker is exhausted, the saturated function is called:

function () {     log (' All workers to is used ');}

The empty function is called when the last task is given to the worker for execution

function () {     log (' No More Tasks wating ');}

When all tasks are finished, the drain function is called

function () {     Console.log (' All tasks has been processed ');}

node. JS Async Process Controller--queue (queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.