0
我想將一個命令傳遞給一個python子進程,然後得到結果。我會使用exec,但是我想讓子進程保持打開狀態,以便每次執行新命令時都不必打開它。這是我的代碼,目前什麼都不做:如何在node.js中獲取子進程操作的結果
var connect = require('connect'),
io = require("socket.io").listen(1032),
util = require("util"),
child = require('child_process'),
python = child.spawn("python");
var app = connect()
.use(connect.static(__dirname + '/www'))
.use(connect.logger('dev'))
.listen(3000);
io.sockets.on('connection', function (socket) {
console.log("Socket " + socket.id + " opened");
python.stdout.on('data', function (data) {
console.log("computed", data.toString("utf-8"));
socket.emit("python", { result : data.toString("utf-8") });
});
socket.on('python', function (data) {
console.log("received data" + data.cmd);
python.stdin.resume();
python.stdin.write(data.cmd);
python.stdin.end();
});
});