Skip to content

Infinite progress bar #60

@sobrinho

Description

@sobrinho

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions