From: Chris Spiegel Date: 2004-06-04T14:18:46+09:00 Subject: Re: playing with popen again On Fri, 04 Jun 2004 04:43 GMT, Hal Fulton wrote: > It hangs indefinitely. When I kill prog1, it appears that I interrupted > a gets on each side. > > # 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 This would be a side-effect of buffering. prog2.rb won't be sending anything over the pipe until it's flushed (standard out is normally line-buffered when connected to a terminal, but fully buffered when it's not). If you add $stdout.flush after each puts in your prog2.rb then it should work as expected. You can check the status of whether autoflushing is on with IO#sync. io.sync in prog1.rb should be true, $stdout.sync in prog2.rb should be false (which means instead of flushing manually, you could do: $stdout.sync = true in prog2.rb.) I'm not extremely well-versed in Ruby so please accept my apologies if this information is more Unix-centric than Ruby-centric. Chris