Created
May 9, 2012 12:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'rake', '0.8.7' | |
gem "retroactive_module_inclusion", "~> 1.2.5" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
["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