https://github.com/freewind/async_demo/blob/master/queue.js
|
// queue(worker, concurrency) |
|
|
var q
= async.queue(function(task, callback) { |
|
|
log('worker
is processing task: ',
task.name); |
|
|
//
如果某次push操作后,任务数将达到或超过worker数量时,将调用该函数 |
|
|
q.saturated
= function() { |
|
|
log('all workers to be
used'); |
|
|
// 当最后一个任务交给worker时,将调用该函数 |
|
|
log('no more tasks
wating'); |
|
|
console.log('all tasks have been
processed'); |
|
|
q.push({name:'t1', run:
function(cb){ |
|
|
log('t1
is running, waiting tasks: ',
q.length()); |
|
|
t.fire('t1',
cb,
400); // 400ms后执行 |
|
|
log('pushed
t1, waiting tasks: ',
q.length()); |
|
|
q.push({name:'t2',run:
function(cb){ |
|
|
log('t2
is running, waiting tasks: ',
q.length()); |
|
|
t.fire('t2',
cb,
200); // 200ms后执行 |
|
|
log('pushed t2, waiting tasks:
', q.length()); |
|
|
q.push([{name:'t3', run: function(cb){ |
|
|
log('t3
is running, waiting tasks: ',
q.length()); |
|
|
t.fire('t3', cb, 300); // 300ms后执行 |
|
|
}},{name:'t4', run: function(cb){ |
|
|
log('t4 is running, waiting tasks:
', q.length()); |
|
|
t.fire('t4',
cb,
500); // 500ms后执行 |
|
log('pushed t3/t4, waiting tasks:
', q.length());
|
var async
= require('async'); |
|
加载中,请稍候......