Skip to content

Instantly share code, notes, and snippets.

@lune-sta
Last active August 29, 2015 14:14
Show Gist options
  • Save lune-sta/2aa41d8f2fb0f300a71c to your computer and use it in GitHub Desktop.
Save lune-sta/2aa41d8f2fb0f300a71c to your computer and use it in GitHub Desktop.
PROCESSORS = (ARGV[0] || 1).to_i
pids = []
workers = PROCESSORS.times.map do |i|
uri = "druby://localhost:#{12345 + i}"
pids << fork { DRb.start_service(uri, MonteCarlo.new(i)); sleep }
DRbObject.new_with_uri(uri)
end
begin
q = Queue.new
sleep 0.1
ts = workers.map do |foo|
Thread.start(foo) {|f| q.push(f.dice(n))}
end
ts.each{|t| t.join}
p q.size.times.map{q.pop}.min
ensure
pids.each {|pid| Process.kill(:TERM, pid)}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment