Skip to content

Instantly share code, notes, and snippets.

@erikh
Forked from AndrewVos/Rakefile
Created August 21, 2012 16:20
Show Gist options
  • Save erikh/3416977 to your computer and use it in GitHub Desktop.
Save erikh/3416977 to your computer and use it in GitHub Desktop.
class ForkPool
attr_accessor :maximum_active_forks
def initialize
@maximum_active_forks = 2
end
def add &block
@blocks ||= []
@blocks << block
end
def start
loop do
break unless @blocks.any?
blocks_to_execute = []
1.upto(maximum_active_forks) { blocks_to_execute << @blocks.shift }
blocks_to_execute.each { |block| Thread.new { Process.wait(fork { block.call }) } }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment