Using threads to show progress

P

Piyush Ranjan

[Note: parts of this message were removed to make it a legal post.]

hmm..yes thats right. Did not think it through. Using queue may not be a
good idea after all as you pointed out. After all queues are meant for
asynchronous stuff and real time status update doesn't actually fit into
that :)

So is using two threads is the best solution ?
 
D

David Masover

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top