I'm downloading images from a (descending) time ordered 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
How about passing a 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 Thread::Pool#process code comment:
Shut down the pool, it will block until all tasks have finished running.
UPDATE
use shutdown! instead of shutdown.
shutown! Shut down the pool instantly without finishing to execute tasks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With