class Bio::MAF::ForkRunner

Public Class Methods

new(n_parallel) click to toggle source
# File lib/bio/maf/jobs.rb, line 18
def initialize(n_parallel)
  @n_parallel = n_parallel
  @jobs = []
  @kids = Set.new
end

Public Instance Methods

add(&proc) click to toggle source
# File lib/bio/maf/jobs.rb, line 24
def add(&proc)
  @jobs << proc
end
run() click to toggle source
# File lib/bio/maf/jobs.rb, line 28
def run
  until @jobs.empty? && @kids.empty?
    while can_start?
      start_job
    end
    await
  end
end

Private Instance Methods

await() click to toggle source
# File lib/bio/maf/jobs.rb, line 63
def await
  pid = Process.wait
  unless @kids.delete?(pid)
    raise "Completion of unexpected job #{pid}!"  
  end
  if ! $?.success?
    raise "Job #{pid} failed with status #{status.exitstatus}!"
  end
end
can_start?() click to toggle source
# File lib/bio/maf/jobs.rb, line 39
def can_start?
  (! @jobs.empty?) && @kids.size < @n_parallel
end
start_job() click to toggle source
# File lib/bio/maf/jobs.rb, line 43
def start_job
  job = @jobs.shift
  pid = fork()
  if pid
    # parent
    @kids << pid
  else
    # child
    begin
      job.call()
      exit 0
    rescue SystemExit
      raise
    rescue Exception
      LOG.error $!
      exit 1
    end
  end
end