Class: Crawlscope::FetchExecutor::Threaded

Inherits:
Object
  • Object
show all
Defined in:
lib/crawlscope/fetch_executor/threaded.rb

Instance Method Summary collapse

Constructor Details

#initialize(concurrency:) ⇒ Threaded

Returns a new instance of Threaded.



8
9
10
# File 'lib/crawlscope/fetch_executor/threaded.rb', line 8

def initialize(concurrency:)
  @concurrency = concurrency
end

Instance Method Details

#call(items) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/crawlscope/fetch_executor/threaded.rb', line 12

def call(items)
  indexed_items = Array(items).each_with_index.to_a
  results = Array.new(indexed_items.size)
  mutex = Mutex.new
  pool = Concurrent::FixedThreadPool.new(@concurrency)

  indexed_items.each do |item, index|
    pool.post do
      result = yield(item)
      mutex.synchronize { results[index] = result }
    end
  end

  pool.shutdown
  pool.wait_for_termination

  results
end