Class: EventMachine::Iterator

Inherits:
Object
  • Object
show all
Includes:
IteratorWithArray, IteratorWithEnumerable
Defined in:
lib/patch/iterator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IteratorWithArray

#next?, #next_item, #setup_list

Methods included from IteratorWithEnumerable

#next?, #next_item, #setup_list

Constructor Details

#initialize(list, concurrency = 1) ⇒ Iterator

Create a new parallel async iterator with specified concurrency.

i = EM::Iterator.new(1..100, 10)

will create an iterator over the range that processes 10 items at a time. Iteration is started via #each, #map or #inject



96
97
98
99
100
101
102
# File 'lib/patch/iterator.rb', line 96

def initialize(list, concurrency = 1)
  @list = setup_list(list)
  @concurrency = concurrency

  @started = false
  @ended = false
end

Instance Attribute Details

#concurrencyObject

Returns the value of attribute concurrency.



113
114
115
# File 'lib/patch/iterator.rb', line 113

def concurrency
  @concurrency
end

Instance Method Details

#each(foreach = nil, after = nil, &blk) ⇒ Object

Iterate over a set of items using the specified block or proc.

EM::Iterator.new(1..100).each do |num, iter|
  puts num
  iter.next
end

An optional second proc is invoked after the iteration is complete.

EM::Iterator.new(1..100).each(
  proc{ |num,iter| iter.next },
  proc{ puts 'all done' }
)

Raises:

  • (ArgumentError)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/patch/iterator.rb', line 129

def each(foreach=nil, after=nil, &blk)
  raise ArgumentError, 'proc or block required for iteration' unless foreach ||= blk
  raise RuntimeError, 'cannot iterate over an iterator more than once' if @started or @ended

  @started = true
  @pending = 0
  @workers = 0

  all_done = proc{
    after.call if after and @ended and @pending == 0
  }

  @process_next = proc{
    # p [:process_next, :pending=, @pending, :workers=, @workers, :ended=, @ended, :concurrency=, @concurrency, :list=, @list]
    unless @ended or @workers > @concurrency
      if next?
        item = next_item
        @pending += 1

        is_done = false
        on_done = proc{
          raise RuntimeError, 'already completed this iteration' if is_done
          is_done = true

          @pending -= 1

          if @ended
            all_done.call
          else
            EM.next_tick(@process_next)
          end
        }
        class << on_done
          alias :next :call
        end

        foreach.call(item, on_done)
      else
        @ended = true
        @workers -= 1
        all_done.call
      end
    else
      @workers -= 1
    end
  }

  spawn_workers

  self
end

#inject(obj, foreach, after) ⇒ Object

Inject the results of an asynchronous iteration onto a given object.

EM::Iterator.new(%w[ pwd uptime uname date ], 2).inject({}, proc{ |hash,cmd,iter|
  EM.system(cmd){ |output,status|
    hash[cmd] = status.exitstatus == 0 ? output.strip : nil
    iter.return(hash)
  }
}, proc{ |results|
  p results
})


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/patch/iterator.rb', line 230

def inject(obj, foreach, after)
  each(proc{ |item,iter|
    is_done = false
    on_done = proc{ |res|
      raise RuntimeError, 'already returned a value for this iteration' if is_done
      is_done = true

      obj = res
      iter.next
    }
    class << on_done
      alias :return :call
      def next
        raise NoMethodError, 'must call #return on an inject iterator'
      end
    end

    foreach.call(obj, item, on_done)
  }, proc{
    after.call(obj)
  })
end

#map(foreach, after) ⇒ Object

Collect the results of an asynchronous iteration into an array.

EM::Iterator.new(%w[ pwd uptime uname date ], 2).map(proc{ |cmd,iter|
  EM.system(cmd){ |output,status|
    iter.return(output)
  }
}, proc{ |results|
  p results
})


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/patch/iterator.rb', line 191

def map(foreach, after)
  index = 0

  inject([], proc{ |results,item,iter|
    i = index
    index += 1

    is_done = false
    on_done = proc{ |res|
      raise RuntimeError, 'already returned a value for this iteration' if is_done
      is_done = true

      results[i] = res
      iter.return(results)
    }
    class << on_done
      alias :return :call
      def next
        raise NoMethodError, 'must call #return on a map iterator'
      end
    end

    foreach.call(item, on_done)
  }, proc{ |results|
    after.call(results)
  })
end