Node js is an asynchronous framework which execute in JavaScript virtual machine chrome v8. It execute very quickly because of asynchronous execution. There are a lot of third party library for node js also. One of the famous is Async module. It provide a lot of methods to control the js code execution flow. This article will show you example about how to use Async module to implement queue object.
If you want to learn basic knowledge about node async module, you can read Node JS Async Module Example first.
1. Node Async Module Queue Example.
Below is the js code to implement queue with node js async module. From the code we can see we create the queue and specify 2 concurrent worker in the queue to execute task at same time.
// Include async module by absolute module install path. var async = require('/usr/local/lib/node_modules/async'); // Specify how many worker execute taks concurrently in the queue. var worker_number = 2; // Create the queue object. The first parameter is a function object. // The second parameter is the worker number that execute task at same time. var queue = async.queue(function (object,callback) { // Get queue start run time. var date = new Date(); var time = date.toTimeString(); // Print task start info. console.log("Start task " + JSON.stringify(object) + " at " + time); // Execute function after object.time timeout. setTimeout(function () { // Get timeout time. date = new Date(); time = date.toTimeString(); // Print task timeout data. console.log("End task " + JSON.stringify(object) + " art " + time); callback(); },object.time) },worker_number) // Loop to add object in the queue with prefix 1. for (var i = 0; i<3; i++) { queue.push({name:"1 - " + i,time:(i+1)*1000},function (err) { console.log(err); }) }; // Loop to add object in the queue with prefix 2. for (var i = 0; i<3; i++) { queue.push({name:"2 - " + i,time:(i+1)*1000},function (err) { console.log(err); }) };
Execute above js code, you can get below output. We can see that only task 1-0 execution finished, task 1-2 can execute. That means only two tasks in the queue execute at same time.
Start task {"name":"1 - 0","time":1000} at 16:24:18 GMT+0800 (CST) Start task {"name":"1 - 1","time":2000} at 16:24:18 GMT+0800 (CST) End task {"name":"1 - 0","time":1000} art 16:24:19 GMT+0800 (CST) undefined Start task {"name":"1 - 2","time":3000} at 16:24:19 GMT+0800 (CST) End task {"name":"1 - 1","time":2000} art 16:24:20 GMT+0800 (CST) undefined Start task {"name":"2 - 0","time":1000} at 16:24:20 GMT+0800 (CST) End task {"name":"2 - 0","time":1000} art 16:24:21 GMT+0800 (CST) undefined Start task {"name":"2 - 1","time":2000} at 16:24:21 GMT+0800 (CST) End task {"name":"1 - 2","time":3000} art 16:24:22 GMT+0800 (CST) undefined Start task {"name":"2 - 2","time":3000} at 16:24:22 GMT+0800 (CST) End task {"name":"2 - 1","time":2000} art 16:24:23 GMT+0800 (CST) undefined End task {"name":"2 - 2","time":3000} art 16:24:25 GMT+0800 (CST) undefined
2. Async Queue Object Methods.
Besides push method, queue object also provide below methods which you can use when you need.
// Invoked when total task number bigger than queue worker number. queue.saturated = function() { console.log("all workers will be used."); } // Invoked when the last task in the queue has been passed to queue worker. queue.empty = function() { console.log("no more waiting tasks."); } // Invoked when all tasks in the queue have been processed. queue.drain = function() { console.log("all tasks have been processed"); } // add items at queue beginning. q.unshift({name: 'java'}, function (err) { console.log('process bar complete'); }); // add an array of items to the queue q.push([{name: 'java'},{name: 'js'},{name: 'node'}], function(err) { console.log('process item complete'); });