Module: YiffSpace::Extensions::Enumerable::Parallel
- Included in:
- Enumerable
- Defined in:
- lib/yiffspace/extensions/enumerable/parallel.rb
Instance Method Summary collapse
-
#parallel_each(executor = :io) ⇒ Object
Like ‘#each`, but perform the block on each item in parallel.
-
#parallel_map(executor = :io) ⇒ Object
Like ‘#map`, but in parallel.
Instance Method Details
#parallel_each(executor = :io) ⇒ Object
Like ‘#each`, but perform the block on each item in parallel. Note that items aren’t processed in order, so things like ‘parallel_each.map` that rely on ordering won’t work.
12 13 14 15 16 17 |
# File 'lib/yiffspace/extensions/enumerable/parallel.rb', line 12 def parallel_each(executor = :io, &) return enum_for(:parallel_each, executor) unless block_given? parallel_map(executor, &) self end |
#parallel_map(executor = :io) ⇒ Object
Like ‘#map`, but in parallel.
20 21 22 23 24 25 26 27 28 |
# File 'lib/yiffspace/extensions/enumerable/parallel.rb', line 20 def parallel_map(executor = :io, &) return enum_for(:parallel_map, executor) unless block_given? promises = map do |item| Concurrent::Promises.future_on(executor, item, &) end Concurrent::Promises.zip_futures_on(executor, *promises).value! end |