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

#The group of running children instances., #group, #state, #statistics

Instance Method Summary collapse

Methods inherited from Forked

multiprocess?, #start

Methods inherited from Generic

#The state of each child instance.=, #[], #async, #failed?, #initialize, #key?, #mark?, #reload, run, #running?, #size, #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, health_check_timeout: nil, **options, &block) ⇒ Object

Run multiple instances of the same block in the container.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/async/container/hybrid.rb', line 19

def run(count: nil, forks: nil, threads: nil, health_check_timeout: 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, health_check_timeout: health_check_timeout, **options, &block)
      
      container.wait_until_ready
      instance.ready!
      
      container.wait
    rescue Async::Container::Terminate
      # Stop it immediately:
      container.stop(false)
      raise
    ensure
      # Stop it gracefully (also code path for Interrupt):
      container.stop
    end
  end
  
  return self
end