On 24.08.2009 22:16, Joel VanderWerf wrote: > Pito Salas wrote: >> This is slightly subtle question about how a process that reads from a >> pipe that is being written to by another process may run asynchronously. >> >> Here's a code snippet: >> >> def start_processing >> open("|-", "r") do |worker| >> if worker >> # here we are in the parent >> i = 0 >> worker.each_line do |line| >> puts "line #{i} >> i = i+1 >> end >> else >> # here we are in child thread >> exec("./iacommand.rb") >> end >> end >> end >> >> iacommand.rb is invoked as a process within ruby. It does a lot of >> processing, outputs a line, does a more processing, outputs another >> line, and so on. >> >> I would like to have the "puts" calls occur right when the a line is >> generated by the script running in the process. >> >> The way it is written above, what happens is that the app waits until >> iacommand.rb exits and then calls the series of puts in immediate >> succession. >> >> So in other words, how do I asynchronously read from a pipe that is >> being fed by a process? > > Hm, the following shows output at 1 sec intervals: > > open("|-", "r") do |worker| > if worker > i = 0 > worker.each_line do |line| > puts "line #{i}:#{line}" > i = i+1 > end > else > exec("ruby -e '3.times {|i| p i; sleep 1}'") > end > end > > Does it work that way for you? Why are you guys using such a complex construction? If you just want to see the output when it comes why not just: system "./iacommand.rb" Or maybe this, if you need to process the output: IO.popen "./iacommand.rb" do |io| io.each_line do |line| puts line end end Note, you may have to do $stdout.sync = true in "iacommand.rb". Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/