Module: InParallel

Includes:
ParallelLogger
Defined in:
lib/in_parallel.rb,
lib/parallel_logger.rb,
lib/in-parallel/version.rb

Defined Under Namespace

Modules: ParallelLogger Classes: InParallelExecutor

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Methods included from ParallelLogger

included

Instance Method Details

#parallel_default_timeoutObject

Gets how many seconds to wait before timing out a forked child process and raising an exception



342
343
344
# File 'lib/in_parallel.rb', line 342

def parallel_default_timeout
  InParallelExecutor.parallel_default_timeout
end

#parallel_default_timeout=(value) ⇒ Object

Sets how many seconds to wait before timing out a forked child process and raising an exception

Parameters:

  • value (Int)

    Time in seconds to wait before timing out and raising an exception



348
349
350
# File 'lib/in_parallel.rb', line 348

def parallel_default_timeout=(value)
  InParallelExecutor.parallel_default_timeout = value
end

#parallel_signal_intervalObject

Gets how many seconds to wait between logging a ‘Waiting for child processes.’



331
332
333
# File 'lib/in_parallel.rb', line 331

def parallel_signal_interval
  InParallelExecutor.parallel_signal_interval
end

#parallel_signal_interval=(value) ⇒ Object

Sets how many seconds to wait between logging a ‘Waiting for child processes.’

Parameters:

  • value (Int)

    Time in seconds to wait before logging ‘Waiting for child processes.’



337
338
339
# File 'lib/in_parallel.rb', line 337

def parallel_signal_interval=(value)
  InParallelExecutor.parallel_signal_interval = value
end

#run_in_background(ignore_result = true, &block) ⇒ Array<Result>, Result

Forks a process for each method within a block and returns immediately.

Example 1 - Will fork a process in the background to execute each method and return immediately:

Parallel.run_in_background do
  @result_1 = method1
  @result_2 = method2
end

Example 2 - Will fork a process in the background to execute each method, return immediately, then later wait for the process to complete, printing it’s STDOUT and assigning return values to instance variables:

Parallel.run_in_background(false) do
  @result_1 = method1
  @result_2 = method2
end
# Do something else here before waiting for the process to complete

wait_for_processes

NOTE: must call wait_for_processes to allow instance variables within the block to be set, otherwise results will evaluate to “unresolved_parallel_result_X”

Parameters:

  • ignore_result (Boolean) (defaults to: true)

    True if you do not care about the STDOUT or return value of the methods executing in the background

  • block (Block)

    This method will yield to a block of code passed by the caller

Returns:

  • (Array<Result>, Result)

    the return values of each method within the block



390
391
392
# File 'lib/in_parallel.rb', line 390

def run_in_background(ignore_result = true, &block)
  InParallelExecutor.run_in_background(ignore_result, &block)
end

#run_in_parallel(timeout = nil, kill_all_on_error = false, &block) ⇒ Array<Result>, Result

Executes each method within a block in a different process.

Example - Will spawn a process in the background to execute each method

Parallel.run_in_parallel do
  @result_1 = method1
  @result_2 = method2
end

NOTE - Only instance variables can be assigned the return values of the methods within the block. Local variables will not be assigned any values.

Parameters:

  • timeout (Int) (defaults to: nil)

    Time in seconds to wait before giving up on a child process

  • kill_all_on_error (Boolean) (defaults to: false)

    Whether to wait for all processes to complete, or fail immediately - killing all other forked processes - when one process errors.

  • block (Block)

    This method will yield to a block of code passed by the caller

Returns:

  • (Array<Result>, Result)

    the return values of each method within the block



364
365
366
367
# File 'lib/in_parallel.rb', line 364

def run_in_parallel(timeout=nil, kill_all_on_error = false, &block)
  timeout ||= InParallelExecutor.parallel_default_timeout
  InParallelExecutor.run_in_parallel(timeout, kill_all_on_error, &block)
end

#wait_for_processes(timeout = nil, kill_all_on_error = false) ⇒ Array<Result>, Result

Waits for all processes started by run_in_background to complete execution, then prints STDOUT and assigns return values to instance variables. See :run_in_background

Parameters:

  • timeout (Int) (defaults to: nil)

    Time in seconds to wait before giving up on a child process

  • kill_all_on_error (Boolean) (defaults to: false)

    Whether to wait for all processes to complete, or fail immediately - killing all other forked processes - when one process errors.

Returns:

  • (Array<Result>, Result)

    the temporary return values of each method within the block



398
399
400
401
# File 'lib/in_parallel.rb', line 398

def wait_for_processes(timeout=nil, kill_all_on_error = false)
  timeout ||= InParallelExecutor.parallel_default_timeout
  InParallelExecutor.wait_for_processes(nil, nil, timeout, kill_all_on_error)
end