Class: Async::Container::Group

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

Instance Method Summary collapse

Constructor Details

#initializeGroup

Returns a new instance of Group.



24
25
26
27
28
29
# File 'lib/async/container/group.rb', line 24

def initialize
  @pgid = nil
  @running = {}
  
  @queue = nil
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


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

def any?
  @running.any?
end

#closeObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/async/container/group.rb', line 82

def close
  begin
    self.kill(:TERM)
  rescue Errno::EPERM
    # Sometimes, `kill` code can give EPERM, if any signal couldn't be delivered to a child. This might occur if an exception is thrown in the user code (e.g. within the fiber), and there are other zombie processes which haven't been reaped yet. These should be dealt with below, so it shouldn't be an issue to ignore this condition.
  end
  
  # Clean up zombie processes - if user presses Ctrl-C or for some reason something else blows up, exception would propagate back to caller:
  interrupt_all
end

#fork(&block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/async/container/group.rb', line 39

def fork(&block)
  self.yield
  
  if pid = ::Process.fork(&block)
    wait_for(pid)
  end
end

#kill(signal = :INT) ⇒ Object



69
70
71
# File 'lib/async/container/group.rb', line 69

def kill(signal = :INT)
  ::Process.kill(signal, -@pgid) if @pgid
end

#sleep(duration) ⇒ Object



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

def sleep(duration)
  self.resume
  self.suspend
  
  Kernel::sleep(duration)
  
  while self.wait_one(::Process::WNOHANG)
  end
end

#spawn(*arguments) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/async/container/group.rb', line 31

def spawn(*arguments)
  self.yield
  
  if pid = ::Process.spawn(*arguments)
    wait_for(pid)
  end
end

#stop(graceful = false) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/async/container/group.rb', line 73

def stop(graceful = false)
  if graceful
    self.kill(:INT)
    interrupt_all
  end
ensure
  self.close
end

#waitObject



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

def wait
  self.resume
  
  while self.any?
    self.wait_one
  end
end