我怀疑/the/program
是缓冲当它检测stdout不是终端 - 你可以通过猫管道测试,例如:
"/the/program" "argument" "argument" | cat
上面的答案,将解决这个问题如果是这样的问题,即:
#!/usr/bin/env ruby
require 'pty'
PTY.spawn "./the-program testing one Two three" do |r,w,p|
loop { puts "GOT: #{r.gets}" }
end
种
某些语言(例如C)检测是否stdout是终端,切换到行缓冲 - 见Is stdout line buffered, unbuffered or indeterminate by default?
作为一个例子时,它的工作原理,我使用的一个简单的bash脚本来输出每个参数和时间,一个在一个时间与之间的3秒和红宝石脚本工作没有问题。我为这个例子添加了eof检测。
修改后的脚本:
#!/usr/bin/env ruby
process = IO.popen(["./the-program", "testing", "one", "Two", "three"])
while !process.eof?
line = process.gets
puts "GOT: #{line}"
end
的节目内容:
#!/bin/bash
for arg
do
echo $arg
date
sleep 3
done
我用Ruby版本1.9.3和2.1.2
$ ruby ,p
GOT: testing
GOT: Mon Jun 16 06:19:00 EST 2014
GOT: one
GOT: Mon Jun 16 06:19:03 EST 2014
GOT: Two
GOT: Mon Jun 16 06:19:06 EST 2014
GOT: three
GOT: Mon Jun 16 06:19:09 EST 2014
$
尝试。如果我使用C程序,不是,那么问题重新出现:
#include <stdio.h>
main(int argc, char **argv)
{
int i;
for (i=0; i<argc; i++) {
printf("%s\n", argv[i]);
sleep(3);
}
}
你试过'process.sync = true'吗? – DNNX
@Eddie你知道了吗? –