From: "Ara.T.Howard" Date: 2004-06-04T22:23:45+09:00 Subject: Re: playing with popen again On Fri, 4 Jun 2004, Hal Fulton wrote: > Why doesn't this work? > > prog1 calls prog2... the latter starts by writing a line, then reading > a line. The former reads first, then writes. > > It hangs indefinitely. When I kill prog1, it appears that I interrupted > a gets on each side. > > What am I doing that's stupid? > > Thanks, > Hal > > > # prog1 > io = IO.popen("ruby prog2.rb","r+") > str = io.gets > puts "got: #{str}" > io.puts "junk" > > str = io.gets > puts "got: #{str}" > io.puts "junk" > > str = io.gets > puts "got: #{str}" > io.puts "junk" > > > #!/usr/local/bin/ruby > # prog2 > puts "This is prompt 1" > gets > puts "This is 2nd prompt" > gets > puts "3rd/final prompt" > gets > > > $ ruby prog1.rb # and then ctl-C > prog1.rb:2:in `gets': Interrupt from prog1.rb:2 > prog2.rb:3:in `gets': Interrupt from prog2.rb:3 stdout is only line buffered when it's a tty. in the case of an external process it is completely buffered - you are seeing the operating system chose not to send the output of prog2 because the buffer has not yet been filled. ~ > cat prog1 io = IO.popen("ruby prog2","r+") 3.times do |i| print "read #{ i } => " str = io.gets puts "<#{ str.inspect }>" io.puts end ~ > cat prog2 # # magic bullet # STDOUT.sync = true 3.times do |i| puts "prompt #{ i }" gets end ~ > ruby prog1 read 0 => <"prompt 0\n"> read 1 => <"prompt 1\n"> read 2 => <"prompt 2\n"> cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; and a weed grows, even though we do | not love it. --Dogen ===============================================================================