带有 node.js 的 Ruby 子进程 [英] Ruby subprocess with node.js

查看:42
本文介绍了带有 node.js 的 Ruby 子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试启动一个 ruby​​ 实例作为我的节点程序的子进程.事实上,一切都很好,但我无法与 ruby​​ 的 STDIN 和 STDOUT 交互.(当然,ruby 程序可以通过我的键盘输入在我的终端中运行)

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

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

这是怎么回事?我已经设法让另一个进程工作......但在这里,没有 IO !什么都没发生!

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

我修改了 ruby​​ 文件以表明,使用 node,该文件仅使用 === 编写,您好!===\n 里面.所以我们可以说,ruby 文件正确启动,但没有从节点接收任何东西(我试图在 STDOUT.write 之后刷新,但 do 语句从未执行.

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.

推荐答案

STDOUT.write之后在ruby端试试STDOUT.flush,因为输出正在缓冲.

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

这篇关于带有 node.js 的 Ruby 子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆