python-shell
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -0,0 +0,0 @@ ## 0.0.3 |
25
index.js
@@ -86,4 +86,5 @@ var EventEmitter = require('events').EventEmitter; | ||
this.childProcess.on('exit', function (code) { | ||
this.childProcess.on('exit', function (code,signal) { | ||
self.exitCode = code; | ||
self.exitSignal = signal; | ||
terminateIfNeeded(); | ||
@@ -93,7 +94,6 @@ }); | ||
function terminateIfNeeded() { | ||
if (!self.stderrHasEnded || !self.stdoutHasEnded || self.exitCode == null) { | ||
return; | ||
} | ||
if(!self.stderrHasEnded || !self.stdoutHasEnded || (self.exitCode == null && self.exitSignal == null)) return; | ||
var err; | ||
if (errorData || self.exitCode !== 0) { | ||
if (errorData || (self.exitCode && self.exitCode !== 0)) { | ||
if (errorData) { | ||
@@ -116,6 +116,7 @@ err = self.parseError(errorData); | ||
} | ||
self.terminated = true; | ||
self.emit('close'); | ||
self._endCallback && self._endCallback(err); | ||
} | ||
self._endCallback && self._endCallback(err,self.exitCode,self.exitSignal); | ||
}; | ||
}; | ||
@@ -251,2 +252,12 @@ util.inherits(PythonShell, EventEmitter); | ||
/** | ||
* Closes the stdin stream, which should cause the process to finish its work and close | ||
* @returns {PythonShell} The same instance for chaining calls | ||
*/ | ||
PythonShell.prototype.terminate = function (signal) { | ||
this.childProcess.kill(signal); | ||
this.terminated = true; | ||
return this; | ||
}; | ||
module.exports = PythonShell; |
{ | ||
"name": "python-shell", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -74,5 +74,8 @@ # python-shell | ||
// end the input stream and allow the process to exit | ||
pyshell.end(function (err) { | ||
pyshell.end(function (err,code,signal) { | ||
if (err) throw err; | ||
console.log('The exit code was: ' + code); | ||
console.log('The exit signal was: ' + signal); | ||
console.log('finished'); | ||
console.log('finished'); | ||
}); | ||
@@ -213,2 +216,6 @@ ``` | ||
#### `.terminate(signal)` | ||
Terminates the python script, the optional end callback is invoked if specified. A kill signal may be provided by `signal`, if `signal` is not specified SIGTERM is sent. | ||
#### event: `message` | ||
@@ -215,0 +222,0 @@ |
@@ -241,5 +241,5 @@ var should = require('should'); | ||
var pyshell = new PythonShell('exit-code.py'); | ||
pyshell.end(function (err) { | ||
pyshell.end(function (err,code,signal) { | ||
if (err) return done(err); | ||
pyshell.exitCode.should.be.exactly(0); | ||
code.should.be.exactly(0); | ||
done(); | ||
@@ -291,2 +291,32 @@ }); | ||
}); | ||
describe('.terminate()', function () { | ||
it('set terminated to true', function (done) { | ||
var pyshell = new PythonShell('infinite_loop.py'); | ||
pyshell.terminate(); | ||
pyshell.terminated.should.be.true | ||
done(); | ||
}); | ||
it('run the end callback if specified', function (done) { | ||
var pyshell = new PythonShell('infinite_loop.py'); | ||
var endCalled = false; | ||
pyshell.end(()=>{ | ||
endCalled = true; | ||
}) | ||
pyshell.terminate(); | ||
pyshell.terminated.should.be.true | ||
done(); | ||
}); | ||
it('terminate with correct kill signal', function (done) { | ||
var pyshell = new PythonShell('infinite_loop.py'); | ||
var endCalled = false; | ||
pyshell.end(()=>{ | ||
endCalled = true; | ||
}) | ||
pyshell.terminate('SIGKILL'); | ||
pyshell.terminated.should.be.true; | ||
setTimeout(()=>{pyshell.exitSignal.should.be.exactly('SIGKILL');},500); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33062
534
269
2