job-pool is yet another module for managing a pool of work and a number of asynchronous workers.
The thing that makes it unusual is that rather than encoding tasks as strings or some other serialized form, jobs are callbacks.
So, if you want a worker to add up some numbers:
{ var result = 1 + 2 + 3 }
You pass that function to the pool:
var JobPool = var jobPool = jobPool
In order for the result to actually be calculated, you need to register a worker:
jobPool.requestWork(
function(task) {
// at the worker's leisure, they can call the function:
task.func(function(result) {
// and then report back the result:
task.callback(result)
// at that point they'll be registered for more work
})
}
)
Retaining workers
If you want to have a single worker do multiple tasks, you can retain them:
var myWorker = jobPool myWorker { } { } {}
Providing data
You can send a data payload to the worker as well, for them to apply to the function:
jobPool jobPool