I want to pass a command to a python child process and then get the result. I would use exec, but I want to keep the child process open so that I don't have to open it every time I do a new command. Here is my code that currently does nothing:
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();
});
});
Does your python code contain any non-ascii characters?
This works fine:
var
spawn = require('child_process').spawn,
python = spawn('python');
python.stdin.write('print ("a")');
python.stdin.end();
python.stdout.on('data', function (data) {
console.log(data.toString());
});
But if I change the letter "a" to a russian letter "п" it stops working. Event not fired.
But it works perfect with node interpreter (with any utf8 characters).
var
spawn = require('child_process').spawn,
node = spawn('node');
node.stdin.write('console.log("п");');
node.stdin.end();
node.stdout.on('data', function (data) {
console.log(data.toString());
});
I think you need to ask about it in python section.