Skip to content

Instantly share code, notes, and snippets.

@ahume

ahume/WorkerPool.js

Created Dec 20, 2011
Embed
What would you like to do?
Example of creating a pool of Web Workers
function WorkerPool(url) {
this.url = url;
this.pool = [];
}
WorkerPool.prototype.getWorker = function() {
var w;
if (this.pool.length > 0) {
w = this.pool.pop();
} else {
w = new Worker(this.url);
}
return w;
}
WorkerPool.prototype.releaseWorker = function(w) {
this.pool.push(w);
}
// Create a new pool
var my_pool = new WorkerPool("fib.js");
// Get a worker from the pool and use it
var worker = my_pool.getWorker();
worker.onmessage = function(e) {
// Do stuff, then release worker if no longer working.
my_pool.releaseWorker(worker);
}
worker.postMessage("start");
@ahume

This comment has been minimized.

Copy link
Owner Author

@ahume ahume commented Dec 20, 2011

Here is a naive pattern for implementing pools of Web Workers, to enable re-use when used heavily. I think this is possibly beneficial for a few reasons.

  • There's a small hit when creating a Worker, which there isn't when popping one from an existing pool.
  • In Webkit (and maybe others?), Worker files are re-requested from the server when creating a new Worker. This could be mitigated to a large degree by properly caching, but still.
  • Chrome (I think) limits the number of Workers allowed to 16, which means you'd have to re-use or queue tasks in someway - but perhaps this can be better handled within one Worker process?

This would also need to set a limit for the number of Workers and allow for queuing tasks until threads are available. Are there existing patterns/code out there for doing this well? Is this even valuable? Or am I doing it wrong?

@akovalev

This comment has been minimized.

Copy link

@akovalev akovalev commented May 1, 2012

hey Andy!
did you find any library/solution which meets your requirements?
I also found your answer on stackoverflow and it looks like I'm looking for something similiar.

@akovalev

This comment has been minimized.

Copy link

@akovalev akovalev commented May 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment