Ruby: Read large data from stdout and stderr of an external process on Windows

Posted by BinaryMuse on Stack Overflow See other posts from Stack Overflow or by BinaryMuse
Published on 2011-01-11T18:51:22Z Indexed on 2011/01/11 18:53 UTC
Read the original article Hit count: 200

Filed under:
|
|

Greetings, all,

I need to run a potentially long-running process from Ruby on Windows and subsequently capture and parse the data from the external process's standard output and error. A large amount of data can be sent to each, but I am only necessarily interested in one line at a time (not capturing and storing the whole of the output).

After a bit of research, I found that the Open3 class would take care of executing the process and giving me IO objects connected to the process's standard output and error (via popen3).

Open3.popen3("external-program.bat") do |stdin, out, err, thread|
  # Step3.profit() ?
end

However, I'm not sure how to continually read from both streams without blocking the program. Since calling IO#readlines on out or err when a lot of data has been sent results in a memory allocation error, I'm trying to continuously check both streams for available input, but not having much luck with any of my implementations.

Thanks in advance for any advice!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about io