Module: Krane::Concurrency

Defined in:
lib/krane/concurrency.rb

Constant Summary collapse

MAX_THREADS =
8

Class Method Summary collapse

Class Method Details

.split_across_threads(all_work, max_threads: MAX_THREADS, &block) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/krane/concurrency.rb', line 6

def self.split_across_threads(all_work, max_threads: MAX_THREADS, &block)
  return if all_work.empty?
  raise ArgumentError, "Block of work is required" unless block_given?

  slice_size = ((all_work.length + max_threads - 1) / max_threads)
  threads = []
  all_work.each_slice(slice_size) do |work_group|
    threads << Thread.new { work_group.each(&block) }
  end
  threads.each(&:join)
end