python-shell-cluster
Version:
Utility to run python script in parallel from nodejs
85 lines • 3.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var python_shell_1 = require("python-shell");
var PythonShellCluster = /** @class */ (function () {
function PythonShellCluster(numberOfinstances, path, options) {
this.queue = [];
this.pyshells = [];
this.scriptLocation = path;
this.addInstances(numberOfinstances, options);
console.log("Object inited");
}
PythonShellCluster.prototype.addInstances = function (n, options) {
for (var i = 0; i < n; i++) {
this.pyshells.push(new python_shell_1.PythonShell(this.scriptLocation, options));
}
console.log("Added " + n + " python-shells instances");
};
PythonShellCluster.prototype.run = function (task) {
var _this = this;
console.log("Adding task to queue: " + task);
return new Promise(function (resolve, reject) {
_this.queue.push({
task: task,
resolve: resolve,
reject: reject,
});
console.log("Trying to execute task");
var instance = _this.pyshells.pop();
if (instance) {
console.log("Instance found");
_this.nextTask(instance);
}
});
};
PythonShellCluster.prototype.runner = function (_a, instance) {
var _this = this;
var task = _a.task, resolve = _a.resolve, reject = _a.reject;
console.log("Running task");
var returnedInstance = instance;
instance.once("message", function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
console.log("message recived: " + args);
resolve(args);
_this.nextTask(returnedInstance);
});
instance.once("stderr", function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
console.log("Error: ", args);
reject(args);
_this.nextTask(returnedInstance);
});
instance.once("close", function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
console.log("instance closed: " + args);
reject(args);
returnedInstance = new python_shell_1.PythonShell(_this.scriptLocation);
_this.nextTask(returnedInstance);
});
instance.send(task);
};
PythonShellCluster.prototype.nextTask = function (instance) {
console.log("looking for next task");
var task = this.queue.pop();
if (task) {
console.log("task found");
this.runner(task, instance);
}
else {
console.log("task not found");
this.pyshells.push(instance);
}
};
return PythonShellCluster;
}());
exports.default = PythonShellCluster;
//# sourceMappingURL=index.js.map