Module: Concurrently

Defined in:
lib/nswtopo/helpers/concurrently.rb

Constant Summary collapse

CORES =
Etc.nprocessors rescue 1

Instance Method Summary collapse

Instance Method Details

#concurrent_groups(threads = CORES, &block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/nswtopo/helpers/concurrently.rb', line 18

def concurrent_groups(threads = CORES, &block)
  group_by.with_index do |item, index|
    index % threads
  end.values.map do |items|
    Thread.new(items, &block)
  end.each(&:join)
end

#concurrently(threads = CORES, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nswtopo/helpers/concurrently.rb', line 4

def concurrently(threads = CORES, &block)
  elements = Queue.new
  threads.times.map do
    Thread.new do
      while element = elements.pop
        block.call element
      end
    end
  end.tap do
    inject(elements, &:<<).close
  end.each(&:join)
  self
end