class MangaDownloadr::Concurrency

Public Class Methods

new(engine_klass = nil, config = Config.new, turn_on_engine = true) click to toggle source
# File lib/manga-downloadr/concurrency.rb, line 5
def initialize(engine_klass = nil, config = Config.new, turn_on_engine = true)
  @engine_klass   = engine_klass
  @config         = config
  @turn_on_engine = turn_on_engine
end

Public Instance Methods

fetch(collection, &block) click to toggle source
# File lib/manga-downloadr/concurrency.rb, line 11
def fetch(collection, &block)
  pool    = Thread.pool(@config.download_batch_size)
  mutex   = Mutex.new
  results = []

  collection.each do |item|
    pool.process {
      engine  = @turn_on_engine ? @engine_klass.new(@config.domain, @config.cache_http) : nil
      reply = block.call(item, engine)&.flatten
      mutex.synchronize do
        results += ( reply || [] )
      end
    }
  end
  pool.shutdown

  results
end

Private Instance Methods

fetch_sequential(collection, &block) click to toggle source

this method is the same as the above but sequential, without Threads it's not to be used in the application, just to be used as a baseline for benchmark

# File lib/manga-downloadr/concurrency.rb, line 34
def fetch_sequential(collection, &block)
  results = []
  engine  = @turn_on_engine ? @engine_klass.new(@config.domain, @config.cache_http) : nil
  collection&.each_slice(@config.download_batch_size) do |batch|
    batch.each do |item|
      batch_results = block.call(item, engine)&.flatten
      results += ( batch_results || [])
    end
  end
  results
end