Class: Async::Container::Threaded

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/threaded.rb

Overview

Manages a reactor within one or more threads.

Instance Method Summary collapse

Constructor Details

#initialize(concurrency: 1, &block) ⇒ Threaded

Returns a new instance of Threaded.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/container/threaded.rb', line 28

def initialize(concurrency: 1, &block)
  @reactors = concurrency.times.collect do
    Async::Reactor.new
  end
  
  @threads = @reactors.collect do |reactor|
    Thread.new do
      Thread.current.abort_on_exception = true
      
      begin
        reactor.run(&block)
      rescue Interrupt
        # Exit cleanly.
      end
    end
  end
end

Instance Method Details

#stopObject



46
47
48
49
# File 'lib/async/container/threaded.rb', line 46

def stop
  @reactors.each(&:stop)
  @threads.each(&:join)
end