带有 Node.js 的 Ruby 子进程
我正在尝试启动一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
STDOUT.write
之后在 ruby 端尝试STDOUT.flush
,因为输出正在被缓冲。
Try
STDOUT.flush
on the ruby side afterSTDOUT.write
,as the output is being buffered.