On Thu, Nov 1, 2012 at 10:06 PM, Matthew Purdy <[email protected]> wrote:

> i have to fork a process and read a large amount of stdout; however, the
> child buffer gets filled up and everything hangs.  how do you read
> stdout as stream?
>
> the only way i know how to do this is the following two ways; but both
> do not work; how can you get the stdout when the method starts so you
> can read stdout as it get written to from the child process?
>
>
You can use a thread:

$ ruby x.rb
1
2
3
4
5
6
7
8
9
10
$ cat -n x.rb
     1
     2
     3  require 'open3'
     4
     5  Open3.popen3("seq", "1", "10") do |s_in, s_out, s_err, th|
     6    # eat stderr
     7    th_err = Thread.new { s = nil; while s_err.read(1024, s); end }
     8
     9    # copy stdout
    10    s_out.each_line {|line| puts line}
    11
    12    # wait until finished
    13    th_err.join
    14  end
    15

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to