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
-
#parallel_default_timeout ⇒ Object
Gets how many seconds to wait before timing out a forked child process and raising an exception.
-
#parallel_default_timeout=(value) ⇒ Object
Sets how many seconds to wait before timing out a forked child process and raising an exception.
-
#parallel_signal_interval ⇒ Object
Gets how many seconds to wait between logging a ‘Waiting for child processes.’.
-
#parallel_signal_interval=(value) ⇒ Object
Sets how many seconds to wait between logging a ‘Waiting for child processes.’.
-
#run_in_background(ignore_result = true, &block) ⇒ Array<Result>, Result
Forks a process for each method within a block and returns immediately.
-
#run_in_parallel(timeout = nil, kill_all_on_error = false, &block) ⇒ Array<Result>, Result
Executes each method within a block in a different process.
-
#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.
Methods included from ParallelLogger
Instance Method Details
#parallel_default_timeout ⇒ Object
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
348 349 350 |
# File 'lib/in_parallel.rb', line 348 def parallel_default_timeout=(value) InParallelExecutor.parallel_default_timeout = value end |
#parallel_signal_interval ⇒ Object
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.’
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”
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.
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
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 |