From: David Masover Date: 2009-12-18T03:50:57+09:00 Subject: Re: Using threads to show progress On Thursday 17 December 2009 11:51:10 am Piyush Ranjan wrote: > So is using two threads is the best solution ? It depends what you mean by "best". The simplest solution, and hardest to get wrong, would be something like this: desc "Go through dictated exams and show IDs" task(:showSomeIDs => :environment) do interval = 0.1 last_update = 0 DictatedExam.all.each do |exam| if Time.now - last_update >= interval puts exam.id last_update = Time.now end end end No threads involved at all. However, this has the problem that you could have several items take much faster than that interval to process, and then one takes a very long time -- in which case, the display would stop updating (which is good), but it would currently show the wrong id, not the id which is taking so long. I don't know that this matters in this case, but if you did want a more accurate update, you'd probably want some sort of thread. But I'd hide that thread in a library, and I might not have it always poll.