Is there a good way to stream results back to the browser from a long-running action? So I''d like to do something like: def my_action stream do |s| s.puts my_header long_running_action_1 s.puts "Long running action 1 complete" long_running_action_2 s.puts "Long running action 2 complete" s.puts my_footer end end Each s.puts would go immediately to the browser. Ideas? Thanks, -- Nathaniel <:((><
Nathaniel Talbott wrote:> Is there a good way to stream results back to the browser from a > long-running action? So I''d like to do something like: > > def my_action > stream do |s| > s.puts my_header > long_running_action_1 > s.puts "Long running action 1 complete" > long_running_action_2 > s.puts "Long running action 2 complete" > s.puts my_footer > end > end > > Each s.puts would go immediately to the browser. Ideas?class Molasses < ActionController::Base def pour render_text do puts header puts wait_div %w(O M G).each { |c| puts c; sleep 10 } puts footer puts hide_wait_div_javascript end end end This will break layouts and anything else which expects the response body to be a String. You can hide the wait div with an inline js snippet or with a body onload handler. Maybe there''s even a non-js way. Note that $stdout is going to the right place. jeremy
On Wed, 2005-02-09 at 12:12 -0500, Nathaniel Talbott wrote:> Is there a good way to stream results back to the browser from a > long-running action? So I''d like to do something like: > > def my_action > stream do |s| > s.puts my_header > long_running_action_1 > s.puts "Long running action 1 complete" > long_running_action_2 > s.puts "Long running action 2 complete" > s.puts my_footer > end > end > > Each s.puts would go immediately to the browser. Ideas?I saw this method on a site recently. A long running process was run separated from the web front end. The visual given was a screen that essentially auto refreshed and checked the status. This also allows your footer to get put up and you don''t risk weird behavior with formatting changing as more structures are sent. -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>
I used to do this but somewhere around IE 5 or 5.5 I started having trouble with the clients automatically aborting before the process was finished. Netscape 4.x would hang forever waiting for the rest of the page to arrive. Anyway, now I do a 5 second refresh until the process is done, reporting stats along the way. A nice advantage to this technique is that if the user logs out, and then logs back in and the process is still running, they get automatically redirected to the status page. (it''s a modal process; you can''t do anything meaningful with the interface until it is done). -Lee On Wed, 9 Feb 2005 12:12:25 -0500, Nathaniel Talbott wrote> Is there a good way to stream results back to the browser from a > long-running action? So I''d like to do something like: > > def my_action > stream do |s| > s.puts my_header > long_running_action_1 > s.puts "Long running action 1 complete" > long_running_action_2 > s.puts "Long running action 2 complete" > s.puts my_footer > end > end > > Each s.puts would go immediately to the browser. Ideas? > > Thanks, > > -- > Nathaniel > > <:((>< > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Naxos Technology
Won''t refreshing require reloading the whole page ? I think xmlhttprequest can be good for this. on the server : store results in a variable and make an action which will access this variable send the results and empties the variable (careful with thread sync if you use thread and don''t want to lose/miss data here) on the client load your initial page, empty, but with a marker givig the end of the data. have a temporized loop run in js every (insert your time here) run an (asynchronous?) xmlhttprequest which will fetch the results from the server. manipulate the dom to insert the new results in your page. and you get the "I use an overhyped technology in my project" for free in addition to limiting bandwidth requirement (okay it is not perfect, if your visitors use old browsers it just won''t work) Jean On Wed, 9 Feb 2005 16:06:00 -0400, LN <ruby-TpSmtbjcwHBsbIuE7sb01tBPR1lH4CV8@public.gmane.org> wrote:> > I used to do this but somewhere around IE 5 or 5.5 I > started having trouble with the clients automatically > aborting before the process was finished. Netscape 4.x > would hang forever waiting for the rest of the page to > arrive. > > Anyway, now I do a 5 second refresh until the process > is done, reporting stats along the way. > > A nice advantage to this technique is that if the user > logs out, and then logs back in and the process is still > running, they get automatically redirected to the status > page. (it''s a modal process; you can''t do anything meaningful > with the interface until it is done). > > -Lee > > On Wed, 9 Feb 2005 12:12:25 -0500, Nathaniel Talbott wrote > > Is there a good way to stream results back to the browser from a > > long-running action? So I''d like to do something like: > > > > def my_action > > stream do |s| > > s.puts my_header > > long_running_action_1 > > s.puts "Long running action 1 complete" > > long_running_action_2 > > s.puts "Long running action 2 complete" > > s.puts my_footer > > end > > end > > > > Each s.puts would go immediately to the browser. Ideas? > > > > Thanks, > > > > -- > > Nathaniel > > > > <:((>< > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > -- > Naxos Technology > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >