From: Dylan Evans Date: 2009-03-19T11:36:40+09:00 Subject: Re: Wrapping applications in Ruby --001485f07c0c0d630e04656fb50d Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit I won't go into details but you should look into pipes in this case. By creating a process which does the work your wrapper code can just wait on the pipe. On Thu, Mar 19, 2009 at 10:13 AM, Jp Hastings-spital wrote: > 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 > -- > Posted via http://www.ruby-forum.com/. > > -- The UNIX system has a command, nice ... in order to be nice to the other users. Nobody ever uses it." - Andrew S. Tanenbaum --001485f07c0c0d630e04656fb50d--