Class: Mdlint::ParallelRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/mdlint/parallel_runner.rb,
sig/internal.rbs

Class Method Summary collapse

Class Method Details

.map(items, jobs: 1) {|item| ... } ⇒ Object

Parameters:

  • items (Object)
  • jobs: (Object) (defaults to: 1)

Yields:

Yield Parameters:

  • item (Object)

Yield Returns:

  • (Object)

Returns:

  • (Object)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mdlint/parallel_runner.rb', line 8

def map(items, jobs: 1)
  return items.map { |item| yield item } unless jobs.to_i > 1 && items.length > 1

  queue = Queue.new
  items.each_with_index { |item, index| queue << [index, item] }
  results = Array.new(items.length)
  errors = Queue.new
  workers = Array.new([jobs.to_i, items.length].min) do
    Thread.new do
      Thread.current.report_on_exception = false
      process_queue(queue, results, errors) { |item| yield item }
    end
  end
  workers.each(&:join)
  raise errors.pop unless errors.empty?

  results
end