Created
January 26, 2010 15:35
-
-
Save cypher/286918 to your computer and use it in GitHub Desktop.
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
# Runs the given array of +commands+ in parallel. The amount of spawned | |
# simultaneous jobs is determined by the `j' env variable and defaults to 1. | |
def parallel_execute(commands) | |
@jobs ||= ENV['j'] ? ENV['j'].to_i : 1 | |
queue = Queue.new | |
queue = commands.each { |command| queue << command } | |
Array.new(@jobs) do | |
Thread.new do | |
while command = queue.shift | |
sh command | |
end | |
end | |
end.each { |t| t.join } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment