padolsey / operative

:dog2: Seamlessly create Web Workers
MIT License
655 stars 45 forks source link

Support a web worker pool #16

Closed ktsuttlemyre closed 10 years ago

ktsuttlemyre commented 10 years ago

It would be nice if I could define a number of web workers to each instance of operative. For example

var lodashWorker = operative(function(method, args, cb) {
    cb(_[method].apply(_, args));
  },
  ['http://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js'],
  4 //number of webworkers in pool
);

That way when you call

for(var i=0,l=10;i<l;i++){
  lodashWorker('uniq', [[1, 2, 3, 3, 2, 1, 4, 3, 2]], function(output) {
      output; // => [1, 2, 3, 4]
  });
}

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!

padolsey commented 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?

evanworley commented 10 years ago

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.