P
Paul Winward
I'm wondering how to capture data written to stdout (or stderr) in real
time using popen3. The problem occurs with the following sample
scripts:
sleep.rb
--------
puts "before sleep"
sleep 3
puts "after sleep"
capture.rb
---------
require 'open3'
Open3.popen3("ruby sleep.rb") do |stdin, stdout, stderr|
Thread.new do
loop do
out = stdout.gets
puts out if out
end
end
sleep 5
end
capture.rb outputs both "before sleep" and "after sleep" after 3 seconds
have passed. When using stderr, however, output is seen in real time.
Note, I'm avoiding STDOUT.flush in sleep.rb since I won't be able to
change the actual scripts I'll be passing to popen3.
Thanks for any help.
Paul
time using popen3. The problem occurs with the following sample
scripts:
sleep.rb
--------
puts "before sleep"
sleep 3
puts "after sleep"
capture.rb
---------
require 'open3'
Open3.popen3("ruby sleep.rb") do |stdin, stdout, stderr|
Thread.new do
loop do
out = stdout.gets
puts out if out
end
end
sleep 5
end
capture.rb outputs both "before sleep" and "after sleep" after 3 seconds
have passed. When using stderr, however, output is seen in real time.
Note, I'm avoiding STDOUT.flush in sleep.rb since I won't be able to
change the actual scripts I'll be passing to popen3.
Thanks for any help.
Paul