Class: GirlFriday::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/girl_friday/batch.rb

Overview

Batch represents a set of operations which can be processed concurrently. Asking for the results of the batch acts as a barrier: the calling thread will block until all operations have completed. Results are guaranteed to be returned in the same order as the operations are given.

Internally a girl_friday queue is created which limits the number of concurrent operations based on the :size option.

TODO Errors are not handled well at all.

Instance Method Summary collapse

Constructor Details

#initialize(enumerable, options, &block) ⇒ Batch

Returns a new instance of Batch.



15
16
17
18
19
20
21
22
23
# File 'lib/girl_friday/batch.rb', line 15

def initialize(enumerable, options, &block)
  @queue = GirlFriday::Queue.new(:batch, options, &block)
  @complete = 0
  @size = enumerable.count
  @results = Array.new(@size)
  @lock = Mutex.new
  @condition = ConditionVariable.new
  start(enumerable)
end

Instance Method Details

#results(timeout = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/girl_friday/batch.rb', line 25

def results(timeout=nil)
  @lock.synchronize do
    @condition.wait(@lock, timeout) if @complete != @size
    @results
  end
end