Class: Crawlscope::FetchExecutor::Async
- Inherits:
-
Object
- Object
- Crawlscope::FetchExecutor::Async
- Defined in:
- lib/crawlscope/fetch_executor/async.rb
Instance Method Summary collapse
- #call(items) ⇒ Object
-
#initialize(concurrency:) ⇒ Async
constructor
A new instance of Async.
Constructor Details
#initialize(concurrency:) ⇒ Async
Returns a new instance of Async.
9 10 11 |
# File 'lib/crawlscope/fetch_executor/async.rb', line 9 def initialize(concurrency:) @concurrency = concurrency end |
Instance Method Details
#call(items) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/crawlscope/fetch_executor/async.rb', line 13 def call(items) indexed_items = Array(items).each_with_index.to_a results = Array.new(indexed_items.size) Sync do |parent| semaphore = ::Async::Semaphore.new(@concurrency) tasks = indexed_items.map do |item, index| semaphore.async(parent: parent) do results[index] = yield(item) end end tasks.each(&:wait) end results end |