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.
# File lib/girl_friday/batch.rb, line 15 def initialize(enumerable=nil, options={}, &block) @queue = GirlFriday::Queue.new(:batch, options, &block) @complete = 0 @size = 0 @results = [] if enumerable @size = enumerable.count @results = Array.new(@size) end @lock = Mutex.new @condition = ConditionVariable.new @frozen = false start(enumerable) end
# File lib/girl_friday/batch.rb, line 39 def push(msg) raise ArgumentError, "Batch is frozen, you cannot push more items into it" if @frozen @lock.synchronize do @results << nil @size += 1 index = @results.size - 1 @queue.push(msg) do |result| completion(result, index) end end end
Generated with the Darkfish Rdoc Generator 2.