Sha256: 62744beff3b21342a5ccfa52e086b0784392bc48e8283b8e1b878c0a256d896f
Contents?: true
Size: 1.18 KB
Versions: 4
Compression:
Stored size: 1.18 KB
Contents
require 'io/wait' ENV['X'] = 'ex' # open a pipe to `sub.rb` for reading and writing fp = IO.popen("ruby #{File.dirname(__FILE__)}/sub.rb", 'r+') # read the initial dump from it sub_output_1 = "" # we need to wait until it's sent data sleep 1 while (not fp.ready?) # then read into `sub_output_1` while it's still has data while fp.ready? # i don't know if i need to read in chunks of 1 or what but that seemed # like a safe choice. there is a also a `read_nonblocking` call that # might be useful instead but looks kinda complicated sub_output_1 += fp.read(1) end # ok, there is no more output waiting # dump what we have puts "*** SUB OUTPUT 1 ***" puts sub_output_1 # this means the pipe is read for input # we now have control back and sub is blocked waiting on our write # change an ENV var now ENV['Y'] = 'why' puts "WRITING...." fp.puts "hey there!" puts "WRITTEN." # that should get sub un-blocked # now repeat the process above to see what it says sub_output_2 = "" sleep 1 while (not fp.ready?) while fp.ready? && (not fp.eof?) sub_output_2 += fp.read(1) end puts "*** SUB OUTPUT 2 ***" puts sub_output_2 # doesn't look like it gets the ENV update # be a good samaritan fp.close
Version data entries
4 entries across 4 versions & 1 rubygems