2016-05-10 8 views
1

Node.jsでPythonShellで実行されるPythonスクリプトの実行を強制終了/停止するにはどうすればよいですか?PythonShellを停止するには

私は対話モードで動作し、出力は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); 
}); 

});

答えて

4

だから、ドキュメントhereに応じPythonShellインスタンスがchild_process.spawnによって作成されたchildProcess属性を持っているし、このanswerに基づいてあなたが

shell.childProcess.kill('SIGINT'); 
を行うことによって、それを殺すことができるはずです