Skip to content

Instantly share code, notes, and snippets.

@cypher
Created January 26, 2010 15:35
Show Gist options
  • Save cypher/286918 to your computer and use it in GitHub Desktop.
Save cypher/286918 to your computer and use it in GitHub Desktop.
# 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