co-queue-koa
Lined up mechanism based on co, run multiple and limit max worker at the same time
API
demo.js
var co = require('co');
var sleep = require('co-sleep');
var Queue = require('../index');
var taskArray = [1,2,3,4,5];
var queue = Queue(function *(task){
yield sleep(task*1000);
console.log('task[%s] begin',task);
return task;
}, 3, true);
queue.push(taskArray);
queue.error = function(err, task){
console.log('task[%s] error: %j', task, err);
};
queue.empty = function(){
console.log('queue empty!');
};
setInterval(function(){
console.log("running task number: %s, tasks length: %s。", queue.worknum, queue.tasks.length);
},1000);
co(function* () {
var res = yield queue.run();
console.log("results:%j", res);
})();
demo stdout:
$ node --harmony test/demo.js
push success, task length: 5
one task begin
one task begin
one task begin
running task number: 3, tasks length: 2。
task[1] begin
one task end
one task begin
running task number: 3, tasks length: 1。
task[2] begin
one task end
one task begin
task[3] begin
running task number: 3, tasks length: 0。
one task end
queue empty!
running task number: 2, tasks length: 0。
task[4] begin
running task number: 2, tasks length: 0。
one task end
running task number: 1, tasks length: 0。
task[5] begin
one task end
results:{"1":1,"2":2,"3":3,"4":4,"5":5}
running task number: 0, tasks length: 0。
running task number: 0, tasks length: 0