Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite progress bar #60

Open
sobrinho opened this issue Jan 9, 2023 · 0 comments
Open

Infinite progress bar #60

sobrinho opened this issue Jan 9, 2023 · 0 comments

Comments

@sobrinho
Copy link

sobrinho commented Jan 9, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant