Skip to content

Instantly share code, notes, and snippets.

@nemtsov
Created March 23, 2015 01:54
Show Gist options
  • Save nemtsov/0d15fddf120b73231fcc to your computer and use it in GitHub Desktop.
Save nemtsov/0d15fddf120b73231fcc to your computer and use it in GitHub Desktop.
ProcessPool.js
var fork = require('child_process').fork;
module.exports = ProcessPool;
function ProcessPool(path, size) {
this.free = [];
this.waiting = [];
this._init(path, size || 1);
}
ProcessPool.prototype.getChild = function (cb) {
var child = this.free.pop();
if (child) cb(null, child);
else this.waiting.push(cb);
};
ProcessPool.prototype.recycle = function (child) {
var waitingCb = this.waiting.pop();
if (waitingCb) waitingCb(null, child);
else this.free.push(child);
};
ProcessPool.prototype._init = function (path, size) {
var i, child;
for (i = 0; i < size; i++) {
child = fork(path);
this.free.push(child);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment