Forum: Ruby Re: IO.write problem on "raw" Windows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brian C. (Guest)
on 2007-06-28 17:11
(Received via mailing list)
> On Windows, when running "normal" ruby from a command prompt (Windows'
> cmd.exe), it blocks on write.
...
> The "other"
> program itself is working fine - it's a simple echo-clone.

I expect the _other_ program is blocking, because you're not reading its
output. That is, you have:

   ruby -------|pipe1|--------> other
        <------|pipe2| --------

Ruby is stuffing data into pipe1, but not reading pipe2. Other program
reads
from pipe1, and stuffs data into pipe2. The O/S will provide some
buffering,
but eventually the other program will block as it can't write any more
to
pipe2.

Simple solution is to have a separate Ruby thread for reading pipe2:
(Note,
not tested under Windows)

prg = "/bin/cat"

pipe = IO.popen(prg, "r+b")
t = Thread.new do
  while line = pipe.gets     # or pipe.read(1024)
    puts "<< #{line}"        # optional
  end
  pipe.close_read
end

File.open(ARGV[0], "rb") do |file|
  pipe.write file.read
end

pipe.close_write
t.join
This topic is locked and can not be replied to.