Brian's Waste of Time

Sat, 22 Apr 2006

Why drbunix:// is Cool

So, ruby threads have issues. Multiprocessing, on the other hand, works great. I have been doing stuff like:

#!/usr/bin/env ruby

require 'drb'
URI = "drbunix:///tmp/foopie"

class Master
    def initialize(count=100)
        @jobs = []
        count.times do |i|
            @jobs << "Job ##{i}"
        end
    end
    
    def take(n)
      taken = []
      while taken.length < n && !@jobs.empty?
        taken << @jobs.shift
      end
      taken
    end
end

DRb.start_service URI, Master.new

pids = []
10.times do |i|
    pids << fork do
        DRb.start_service
        master = DRbObject.new_with_uri URI
        until (jobs = master.take(2)).empty?
          jobs.each do |j| 
            puts j
            sleep rand
          end
        end
    end
end

puts "started children" 
pids.each { |pid| Process.wait(pid) }

To slam though jobs in ruby. Doing it over unix sockets instead of tcp is just nicer =) It just spreads the workload across processes instead of threads.

1 writebacks [/src/ruby] permanent link

DRb over Unix Sockets

So, it is right in the docs, but somehow I overlooked it... You can do DRb over unix sockets dirt easily =)

require 'drb'

class Logger
  def log m
    puts m
  end
end

DRb.start_service "drbunix:///tmp/logger", Logger.new

Note the URI.

require 'drb'

DRb.start_service

logger = DRbObject.new_with_uri "drbunix:///tmp/logger"

logger.log "Woot!"

=)

2 writebacks [/src/ruby] permanent link