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) }

Parameters:

  • identifier (String) (defaults to: nil)
    • Optional identifier for logging purposes only. Will use the block location by default.

  • timeout (Int) (defaults to: (InParallel::InParallelExecutor.parallel_default_timeout))
    • Seconds to wait for a forked process to complete before timing out

Returns:

  • (Array<Object>)

    results - the return value of each block execution.



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)
  else
    # If fork is not supported
    map(&block)
  end
end