J
Jp Hastings-spital
Variants on this theme have been posted time and again, but I can't find
a solution for this specific problem:
Say I have a program I need to use that performs a task for a given
amount of time then finishes, all the while providing a 'percent
complete' style output on STDOUT. eg:
/counting.rb
[RUBY]
#!/usr/bin/env ruby
n = 0
while n <= 10
$stdout.puts "Completed: #{n*10}%"
n = n + 1
end
[/RUBY]
Now, I'd like to build a ruby script that will call this script, wait
until completion and then continue with its tasks BUT all the while
updating a variable with the percentage complete. eg:
/script.rb
[RUBY]
def updatePercentage(pc)
p "The percentage is now #{pc}"
end
Thread.new {
# -- Problem area
# Attempt 1
`./counting.rb`
# This will wait until after counting.rb is complete before
# "All Done!" is printed, but I can't get to its stdout stream
# before the program has finished running
# Attempt 2
Open3.popen3('./counting.rb') { |stdin, stdout, stderr|
p stdout.readpartial(1)
# With a view to doing something like:
updatePercentage(stdout.readline.gsub(/^.*([0-9]+)%.*$/,"$1"))
}
# This doesn't work, counting.rb never begins executing
# (tested by adding a line: open('output.txt','w') {|file| file.puts
"Percentage: #{n*10}%" }
# to counting.rb and watching the file externally)
p "All done!"
}
p "Irrelevant things"
sleep
[/RUBY]
Any ideas anyone? Essentially I'm looking for ways to wrap C programs
into ruby.
Cheers, JP
a solution for this specific problem:
Say I have a program I need to use that performs a task for a given
amount of time then finishes, all the while providing a 'percent
complete' style output on STDOUT. eg:
/counting.rb
[RUBY]
#!/usr/bin/env ruby
n = 0
while n <= 10
$stdout.puts "Completed: #{n*10}%"
n = n + 1
end
[/RUBY]
Now, I'd like to build a ruby script that will call this script, wait
until completion and then continue with its tasks BUT all the while
updating a variable with the percentage complete. eg:
/script.rb
[RUBY]
def updatePercentage(pc)
p "The percentage is now #{pc}"
end
Thread.new {
# -- Problem area
# Attempt 1
`./counting.rb`
# This will wait until after counting.rb is complete before
# "All Done!" is printed, but I can't get to its stdout stream
# before the program has finished running
# Attempt 2
Open3.popen3('./counting.rb') { |stdin, stdout, stderr|
p stdout.readpartial(1)
# With a view to doing something like:
updatePercentage(stdout.readline.gsub(/^.*([0-9]+)%.*$/,"$1"))
}
# This doesn't work, counting.rb never begins executing
# (tested by adding a line: open('output.txt','w') {|file| file.puts
"Percentage: #{n*10}%" }
# to counting.rb and watching the file externally)
p "All done!"
}
p "Irrelevant things"
sleep
[/RUBY]
Any ideas anyone? Essentially I'm looking for ways to wrap C programs
into ruby.
Cheers, JP