From: ara.t.howard@... Date: 2006-04-14T04:19:16+09:00 Subject: Re: IO stream help On Fri, 14 Apr 2006, Lon Baker wrote: > Thanks for all the help. > > I came up with the following code that works. > > run = true > > $stdout.sync = true > > while run > input,output,error = IO::select( [$stdin], nil, nil, nil ) > (input||[]).each {|s| > chunks = s.readline.split > case chunks[1] > when /INTF/ > $stdout.puts "#{chunks[0]} #{chunks[1]} #{chunks[2]}" > when /QUIT/ > $stdout.puts "#{chunks[0]} OK" > run = false > else > $stdout.puts "#{chunks[0]} OK" > end > } > end > > This is used in a helper application for Communigate Pro mail servers. but this is blocking? we thought you were needing asynchronous code? what you've written above will behave __exactly__ like STDOUT.sync = true STDIN.each do |s| chunks = s.readline.split case chunks[1] when /INTF/ puts "#{chunks[0]} #{chunks[1]} #{chunks[2]}" when /QUIT/ puts "#{chunks[0]} OK" else puts "#{chunks[0]} OK" end end that's because what you've got above does this # # here we block until io is ready to read. note that this can be because # there is one char available or eof. # input,output,error = IO::select( [$stdin], nil, nil, nil ) # # here we block until a newline is read. note that a newline may never be # read, or may take a very long time to arrive. esp if the co-process is # buffering io. and that's for the first iteration of the loop. on the # second iteration this loop degrades into a 'normally' blocking call (note # that select is not called again). # (input||[]).each {|s| in otherwords what you've written is a compilcated way to do this # # block until io ready. then block until a newline is read. # gets not trying to be critical, but after reading your original posts the above left me confused. is there some other thread doing some work that must procedd that's not being shown? regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama