Closed ktsuttlemyre closed 10 years ago
Interesting proposal! Not sure of the best way to determine if a worker is available though -- or even if that's necessary. E.g. I can create an operative pool quite easily, which takes the next one in the queue (looping round):
// Same arg signature as you recommended:
function operativePool(module, deps, size) {
var operatives = [];
var current = 0;
size = size || 1;
while (size--) {
operatives.push(operative(module, deps));
}
return function() {
var cur = current;
current = current + 1 >= operatives.length ? 0 : current + 1;
return operatives[cur].apply(null, arguments);
};
}
I guess an ideal implementation would message a worker, and if it responds within (e.g.) 10-30ms, it gives it the job, otherwise it asks the next one. Does that sound closer to what you wanted?
We also have a need to use a pool of operatives, and for these we use a resource pool similar to the one @padolsey has show above.
It would be nice if I could define a number of web workers to each instance of operative. For example
That way when you call
whatever worker is available will run the code
This could be implemented where order is guaranteed or order is indeterminate
BTW Love the way your project is implemented. I appreciate the simplicity, documentation and technical descriptions!