Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby subprocess with node.js

I'm trying to launch a ruby instance as subprocess of my node program. In fact, everything is OK but I just can't interact with the ruby's STDIN and STDOUT. (of course the ruby program works in my terminal with my keyboard input)

So this is a simplified code that I want to get working ...

simpleproc.js

var util   = require('util'),
    spawn = require('child_process').spawn,
    ruby  = spawn('ruby', [__dirname + '/process.rb']);

ruby.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

ruby.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ruby.on('exit', function (code) {
  console.log('child process exited with code ' + code);
});

ruby.stdin.write("ping\n");

process.rb

f = File.new("process.log", "w")
f.write "=== Hello! ===\n"
STDIN.each_line do |line|
   STDOUT.write line
   f.write line
end

What's wrong with it ? I've already managed to get an another process working... but here, there is no IO ! Nothing happens !

EDIT: I modified the ruby file to show that, with node, the file is only written with === Hello! ===\n inside. So we can say that, the ruby file is correctly launched but doesn't receive anything from node (I've tried to flush after the STDOUT.write but the do statement is never executed.

like image 295
ricard.robin Avatar asked Dec 03 '25 18:12

ricard.robin


1 Answers

Try STDOUT.flush on the ruby side after STDOUT.write, as the output is being buffered.

like image 76
michaelku Avatar answered Dec 05 '25 06:12

michaelku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!