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, name: nil, &block) ⇒ Threaded

Returns a new instance of Threaded.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/async/container/threaded.rb', line 30

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(&block)
			rescue Interrupt
				# Exit cleanly.
			end
		end
	end
	
	@finished = nil
end

Instance Method Details

#stopObject



61
62
63
64
65
# File 'lib/async/container/threaded.rb', line 61

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

#waitObject



53
54
55
56
57
58
59
# File 'lib/async/container/threaded.rb', line 53

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