I guess you should make sure to flush the pipe caches before trying to
get output from the background process. If the data you sent in the
first place is still sitting there, you won't get anything back...

Anyway, I had better luck with using two separate threads for writing
and reading to/from the pipe. This one below just hangs because the
buffers fill up and it can't send more data before reading from the
output. Note that this is on Cygwin on WinXP SP2 - the amount of data
which triggers the deadlock may wary on other systems.

open('|tr [a-z] [A-Z]', 'w+') do |tr|
	30000.times{tr.puts 'hehe'}
	tr.close_write
	puts tr.read
end

But this one echoes back the HEHE's just as expected:

open('|tr [a-z] [A-Z]', 'w+') do |tr|
	Thread.new do
		30000.times{tr.puts 'hehe'}
		tr.close_write
	end
	puts tr.read
end

Alexy Khrabrov wrote:
> Hey Szymon -- good to see you here!  If, instead of "grep something", I 
> give it a Ruby program which reads and writes lines, it works OK.  I 
> wonder what's different between these: