带有 Node.js 的 Ruby 子进程

发布于 2024-10-24 04:39:33 字数 982 浏览 1 评论 0原文

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

所以这是我想要运行的简化代码...

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 !什么也没有发生!

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-10-31 04:39:33

STDOUT.write 之后在 ruby​​ 端尝试 STDOUT.flush
因为输出正在被缓冲。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文