Class: Async::Container::Hybrid

Inherits:
Forked show all
Defined in:
lib/async/container/hybrid.rb

Overview

Provides a hybrid multi-process multi-thread container.

Constant Summary

Constants inherited from Generic

Generic::UNNAMED

Instance Attribute Summary

Attributes inherited from Generic

#state, #statistics

Instance Method Summary collapse

Methods inherited from Forked

multiprocess?, #start

Methods inherited from Generic

#[], #async, #failed?, #initialize, #key?, #mark?, #reload, run, #running?, #sleep, #spawn, #status?, #stop, #to_s, #wait, #wait_until_ready

Constructor Details

This class inherits a constructor from Async::Container::Generic

Instance Method Details

#run(count: nil, forks: nil, threads: nil, **options, &block) ⇒ Object

Run multiple instances of the same block in the container.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/container/hybrid.rb', line 34

def run(count: nil, forks: nil, threads: nil, **options, &block)
	processor_count = Container.processor_count
	count ||= processor_count ** 2
	forks ||= [processor_count, count].min
	threads = (count / forks).ceil
	
	forks.times do
		self.spawn(**options) do |instance|
			container = Threaded.new
			
			container.run(count: threads, **options, &block)
			
			container.wait_until_ready
			instance.ready!
			
			container.wait
		rescue Async::Container::Terminate
			# Stop it immediately:
			container.stop(false)
		ensure
			# Stop it gracefully (also code path for Interrupt):
			container.stop
		end
	end
	
	return self
end