Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created May 9, 2012 12:35
Show Gist options
  • Save jeroenr/2644211 to your computer and use it in GitHub Desktop.
Save jeroenr/2644211 to your computer and use it in GitHub Desktop.
Implementation of an asynchronous execution of blocks on items in a collection, applied to enumerable using monkey patching
module DebDeploy
module AsynchronousCollection
def each(&block)
threads = []
super do |item|
threads << Thread.new { yield item }
end
threads.each(&:join)
end
def map(&block)
super do |item|
Thread.new { Thread.current[:output] = block[item] }
end.map(&:join).map { |thread| thread[:output] }
end
end
end
source :rubygems
gem 'rake', '0.8.7'
gem "retroactive_module_inclusion", "~> 1.2.5"
require 'deb_deploy/util/collection_utils'
module CoreExt
module Enumerable
module Parallellization
def async
self.clone.extend DebDeploy:AsynchronousCollection
end
end
end
end
Enumerable.send :retroactively_include, CoreExt::Enumerable::Parallellization
["aa","bb"].async.each{|x| x.chop }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment