-
-
Save erikh/3416977 to your computer and use it in GitHub Desktop.
This file contains 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
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