From: Joel VanderWerf Date: 2009-08-25T05:16:39+09:00 Subject: Re: Asynchronous process to process pipe IO 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? -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407