paul/progress_bar

Infinite progress bar

sobrinho opened this issue · 1 comments

Hi,

I have a scenario where I'm iterating through a cursor but while I'm iterating I'm already processing the results.

So, there is a considerable delay between the scanning and the final, here's a pseudo-example:

x = Queue.new
bar = ProgressBar.new(Float::INFINITY)

Thread.new do
  i = 0

  cursor.each do |foo|
    x << foo
  end

  bar.total = i
  x.close
end

Thread.new do
  while (item = x.pop)
    process(item)
    bar.increment!
  end
end

Would be nice to have the progress bar in a state that says how many were processed but not the total/ETA yet while that.

Otherwise, I have to implement some dark magic to wait until the scan is done:

x = Queue.new
mutex = Mutex.new
processed = 0
total = 0
bar = nil

Thread.new do
  cursor.each do |foo|
    x << foo
    total += 1
  end

  mutex.synchronize do
    bar = ProgressBar.new(total)
    bar.increment!(processed)
  end
end

10.times do
  Thread.new do
     while (item = x.pop)
      process(item)
      mutex.synchronize do
        processed += 1
        bar&.increment!
      end
    end
  end
paul commented

I'd be willing to take a look at a PR that treated a max of nil as special, and not do the percent or ETA calculations that depend on knowing the max...