Class: Green::Pool

Inherits:
Group
  • Object
show all
Defined in:
lib/green/group.rb

Instance Attribute Summary collapse

Attributes inherited from Group

#greens, #options

Instance Method Summary collapse

Methods inherited from Group

#add, #apply, #discard, #enumerator, #kill, #size

Constructor Details

#initialize(*args) ⇒ Pool

Returns a new instance of Pool.

Raises:

  • (ArgumentError)


71
72
73
74
75
# File 'lib/green/group.rb', line 71

def initialize(*args)
  super
  raise ArgumentError.new("Undefined option :size") unless options[:size]
  @semaphore = Semaphore.new(options[:size])
end

Instance Attribute Details

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



70
71
72
# File 'lib/green/group.rb', line 70

def semaphore
  @semaphore
end

Instance Method Details

#joinObject



88
89
90
# File 'lib/green/group.rb', line 88

def join
  semaphore.wait
end

#spawn(*args, &blk) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/green/group.rb', line 77

def spawn(*args, &blk)
  semaphore.acquire
  super() do
    begin
      blk.call(*args)
    ensure
      semaphore.release
    end
  end
end