Break out of the threaded loop

I am loading images from (descending) time sorted gallery. I want to stop when we get to the pictures already down.

require 'thread/pool'

def getimg(uri)
#...
  if File.exist? filename
    raise "Already done."  # something like this
  end
#...
end

pool = Thread.pool(4)

thumbs.each do |a|
  pool.process {
    getimg(URI(a.attr('href')))
  }
end

      

+3


source to share


1 answer


How to skip the pool object and use pool.shutdown

?

require 'thread/pool'

def getimg(uri, pool) # <----
#...
  if File.exist? filename
    pool.shutdown  # <--------
    return         # <------
  end
#...
end

pool = Thread.pool(4)

thumbs.each do |a|
  pool.process {
    getimg(URI(a.attr('href')), pool) # <----
  }
end

      

According to the code commentThread::Pool#process

:

Turn off the pool, it will be blocked until all tasks are completed.



UPDATE

use shutdown!

instead shutdown

.

shutdown! Shut down the pool immediately without finishing the tasks.

+2


source







All Articles