Sha256: 1b9372dd4846e06d9682af72648b94af6c8aca2618bde15c137b2a1a159e6051
Contents?: true
Size: 1.45 KB
Versions: 9
Compression:
Stored size: 1.45 KB
Contents
require "singleton" require "thread" class LimitedFork include Singleton def initialize @pids = 0 @max_processes = 10 # arbitrary default @mutex=Mutex.new trap("CHLD") do begin loop do pid = Process.wait(0) @mutex.synchronize do # @pids -= [ pid ] @pids -= 1 end end rescue Errno::ECHILD @pids = 0 end end end attr_accessor :pids, :pids, :max_processes, :mutex def self.pids instance.pids end def self.pids=(val) instance.pids=val end def self.limit=(limit) instance.max_processes=limit end def self.fork if pids > instance.max_processes then # block until it's okay to go begin pid = Process.waitpid instance.mutex.synchronize do # instance.pids -= [ pid ] self.pids -= 1 end rescue Errno::ECHILD # Oops, look like someone already got there. Never # mind then! self.pids=0 end end pid = Process.fork { yield } if pid instance.mutex.synchronize do instance.pids += 1 end end return pid end end
Version data entries
9 entries across 9 versions & 1 rubygems