Skip to content

Commit 8505392

Browse files
committed
Implemented a terminate function
Terminates the child process and calls the end callback
1 parent 0daee5f commit 8505392

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,15 @@ PythonShell.prototype.end = function (callback) {
245245
return this;
246246
};
247247

248+
/**
249+
* Closes the stdin stream, which should cause the process to finish its work and close
250+
* @returns {PythonShell} The same instance for chaining calls
251+
*/
252+
PythonShell.prototype.terminate = function () {
253+
this.childProcess.kill();
254+
this.terminated = true;
255+
this._endCallback && this._endCallback();
256+
return this;
257+
};
258+
248259
module.exports = PythonShell;

test/python/infinite_loop.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a = 0
2+
while(True):
3+
a += 1

test/test-python-shell.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,23 @@ describe('PythonShell', function () {
287287
});
288288
});
289289
});
290+
291+
describe('.terminate()', function () {
292+
it('set terminated to true', function (done) {
293+
var pyshell = new PythonShell('infinite_loop.py');
294+
pyshell.terminate();
295+
pyshell.terminated.should.be.true;
296+
done();
297+
});
298+
it('run the end callback if specified', function (done) {
299+
var pyshell = new PythonShell('infinite_loop.py');
300+
var endCalled = false;
301+
pyshell.end(()=>{
302+
endCalled = true;
303+
})
304+
pyshell.terminate();
305+
endCalled.should.be.true;
306+
done();
307+
});
308+
});
290309
});

0 commit comments

Comments
 (0)