我怎样才能停止PythonShell

我怎样才能杀死/停止在Node.js中执行由PythonShell执行的Python脚本?

我运行在交互模式,输出通过socket.io发射到一个给定的房间。 如果没有更多的客户端连接到这个房间,我想停止python脚本执行。 这是我的代码片段。 谢谢。

app.get('/live', function(req, res) { var room = req.query.room; var shell = new pythonShell('test_live.py'); shell.on('message', function(message) { var clientNumber = 0; if ( room in io.sockets.adapter.rooms){ clientNumber = io.sockets.adapter.rooms[room].length; } console.log(clientNumber); if (clientNumber> 0){ // handle message (a line of text from stdout) io.to(room).emit('send:logentry', { logentry: room + " " + message }); }else{ // I want to kill the python script execution } console.log("Send to" + room + ": " + message); }); // end the input stream and allow the process to exit shell.end(function(err) { if (err) throw err; res.sendStatus(200); }); 

});

所以根据这里的文档, PythonShell实例有一个由child_process.spawn创build的childProcess属性,然后基于这个答案,你应该能够通过做

 shell.childProcess.kill('SIGINT');