Skip to content

Commit db1cd11

Browse files
committed
Added ability to send kill signal to terminate and address #94
1 parent 54a3f36 commit db1cd11

File tree

1 file changed

+9
-27
lines changed

1 file changed

+9
-27
lines changed

index.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,48 +73,31 @@ var PythonShell = function (script, options) {
7373
errorData += ''+data;
7474
});
7575

76-
this.stderr.on('end', function(){
77-
self.stderrHasEnded = true
78-
terminateIfNeeded();
79-
})
80-
81-
this.stdout.on('end', function(){
82-
self.stdoutHasEnded = true
83-
terminateIfNeeded();
84-
})
85-
86-
this.childProcess.on('exit', function (code) {
87-
self.exitCode = code;
88-
terminateIfNeeded();
89-
});
90-
91-
function terminateIfNeeded() {
92-
if (!self.stderrHasEnded || !self.stdoutHasEnded || self.exitCode == null) {
93-
return;
94-
}
76+
this.childProcess.on('exit', function (code, signal) {
9577
var err;
96-
if (errorData || self.exitCode !== 0) {
78+
if (errorData || (code && code !== 0)) {
9779
if (errorData) {
9880
err = self.parseError(errorData);
9981
} else {
100-
err = new Error('process exited with code ' + self.exitCode);
82+
err = new Error('process exited with code ' + code);
10183
}
10284
err = extend(err, {
10385
executable: pythonPath,
10486
options: pythonOptions.length ? pythonOptions : null,
10587
script: self.script,
10688
args: scriptArgs.length ? scriptArgs : null,
107-
exitCode: self.exitCode
89+
exitCode: code
10890
});
10991
// do not emit error if only a callback is used
11092
if (self.listeners('error').length || !self._endCallback) {
11193
self.emit('error', err);
11294
}
11395
}
96+
11497
self.terminated = true;
11598
self.emit('close');
116-
self._endCallback && self._endCallback(err);
117-
}
99+
self._endCallback && self._endCallback(err,self.exitCode,signal);
100+
});
118101
};
119102
util.inherits(PythonShell, EventEmitter);
120103

@@ -249,10 +232,9 @@ PythonShell.prototype.end = function (callback) {
249232
* Closes the stdin stream, which should cause the process to finish its work and close
250233
* @returns {PythonShell} The same instance for chaining calls
251234
*/
252-
PythonShell.prototype.terminate = function () {
253-
this.childProcess.kill();
235+
PythonShell.prototype.terminate = function (signal) {
236+
this.childProcess.kill(signal);
254237
this.terminated = true;
255-
this._endCallback && this._endCallback();
256238
return this;
257239
};
258240

0 commit comments

Comments
 (0)