Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out from threaded loop

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
like image 818
Kambus Avatar asked Aug 29 '26 21:08

Kambus


1 Answers

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.

like image 116
falsetru Avatar answered Aug 31 '26 18:08

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!