Class: PeerCommander::ParallelExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/peer_commander/parallel_executor.rb

Overview

Executes a given set of commands with a specified parallelism

Constant Summary collapse

SLEEP_DURATION =
0.2

Instance Method Summary collapse

Constructor Details

#initializeParallelExecutor

Returns a new instance of ParallelExecutor.



8
9
10
11
# File 'lib/peer_commander/parallel_executor.rb', line 8

def initialize
  @futures = []
  @command_results = []
end

Instance Method Details

#execute(commands, parallelism) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/peer_commander/parallel_executor.rb', line 13

def execute(commands, parallelism)
  raise ArgumentError, "Parallelism must be at least 1" if parallelism < 1

  @parallelism = parallelism

  commands.each do |command|
    wait_for_slot if all_slots_filled?

    @futures << Concurrent::Future.execute { command.execute }
  end

  wait_for_all

  command_results
end