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.

Defined Under Namespace

Classes: Instance

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Threaded.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/container/threaded.rb', line 40

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

Instance Method Details

#stopObject



71
72
73
74
75
# File 'lib/async/container/threaded.rb', line 71

def stop
  @reactors.each(&:stop)
  
  wait
end

#waitObject



63
64
65
66
67
68
69
# File 'lib/async/container/threaded.rb', line 63

def wait
  return if @finished
  
  @threads.each(&:join)
    
  @finished = true
end