Class: Ji2p::ConcurrentExecutor::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/ji2p/concurrent_executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(pool: nil) ⇒ Future

Returns a new instance of Future.



20
21
22
23
# File 'lib/ji2p/concurrent_executor.rb', line 20

def initialize(pool: nil)
  @pool = pool || Concurrent::FixedThreadPool.new(20)
  @exceptions = Concurrent::Array.new
end

Instance Method Details

#execute(array, &block) ⇒ Object

Sample Usage executor = ConcurrentExecutor::Future.new(pool: pool) executor.execute(carriers) do | carrier |

...

end

values = executor.resolve



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ji2p/concurrent_executor.rb', line 33

def execute array, &block
  @futures = array.map do | element |
    Concurrent::Future.execute({ executor: @pool }) do
      yield(element)
    end.rescue do | exception |
      @exceptions << exception
    end
  end

  self
end

#resolveObject



45
46
47
48
49
50
51
52
53
# File 'lib/ji2p/concurrent_executor.rb', line 45

def resolve
  values = @futures.map(&:value)

  if @exceptions.length > 0
    raise ConcurrentExecutor::Error.new(@exceptions)
  end

  values
end