cargoQueue()
is a method used in concurrent programming with the Async module. cargoQueue()
creates a cargoQueue
object that can be used to manage tasks. We specify the payload and number of
Tasks are added to the cargoQueue
object and processed together (up to the payload limit). The parameter concurrency
(see below) determines the number of parallel workers. If all workers
are in progress, the task is queued until one becomes available. Once a worker
has completed some tasks, each callback of those tasks is called.
cargoQueue
is essentially a combination of the cargo
and queue
objects. The animations below show how cargo
and queue
work.
While queue only passes one task to one group of workers at a time, and cargo passes an array of tasks to a single worker, the cargoQueue passes an array of tasks to multiple parallel workers.
cargoQueue(worker, concurrency, payload)
worker
: An asynchronous function for processing an array of queued tasks invoked with (tasks, callback)
.concurrency
: (Optional) The number of worker
functions to run in parallel. The default value is .payload
: (Optional) The number of tasks to be processed per round. The default is unlimited.Returns a cargoQueue
object to manage the tasks. Callbacks can be attached as certain properties to listen for specific events during the lifecycle of the cargoQueue and inner queue.
In the following example, we create a cargoQueue
object with concurrency=2
and payload=2
. We pass an async function (worker) that takes an array of tasks and outputs each task’s name.
To add tasks into the cargoQueue
we use cargoQueue.push()
.
import async from 'async';// create a cargoQueue object with concurrency 2 and payload 2var cargoQueue = async.cargoQueue(function(tasks, callback) {for (var i=0; i<tasks.length; i++) {console.log('hello ' + tasks[i].name);}callback();}, 2, 2);// add some itemscargoQueue.push({name: 'foo'}, function(err) {console.log('finished processing foo');});cargoQueue.push({name: 'bar'}, function(err) {console.log('finished processing bar');});cargoQueue.push({name: 'baz'}, function(err) {console.log('finished processing baz');});cargoQueue.push({name: 'boo'}, function(err) {console.log('finished processing boo');});
Free Resources