Module: Enumerable

Defined in:
lib/parallel_enumerable.rb

Overview

Extending Enumerable to make it easy to do any .each in parallel

Instance Method Summary collapse

Instance Method Details

#each_in_parallel(identifier = nil, timeout = (InParallel::InParallelExecutor.parallel_default_timeout), kill_all_on_error = false, &block) ⇒ Array<Object>

Executes each iteration of the block in parallel

Example - Will execute each iteration in a separate process, in parallel, log STDOUT per process, and return an array of results.

my_array = [1,2,3]
my_array.each_in_parallel { |int| my_method(int) }


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/parallel_enumerable.rb', line 11

def each_in_parallel(identifier=nil, timeout=(InParallel::InParallelExecutor.parallel_default_timeout), kill_all_on_error = false, &block)
  if InParallel::InParallelExecutor.fork_supported? && count > 1
    identifier ||= "#{caller[0]}"
    each do |item|
      InParallel::InParallelExecutor._execute_in_parallel(identifier) { block.call(item) }
    end
    # return the array of values, no need to look up from the map.
    return InParallel::InParallelExecutor.wait_for_processes(nil, block.binding, timeout, kill_all_on_error)
  end
  # If fork is not supported
  block.call
  each(&block)
end