Skip to content

Instantly share code, notes, and snippets.

@alexbartlow
Created April 18, 2010 18:02
Show Gist options
  • Save alexbartlow/370438 to your computer and use it in GitHub Desktop.
Save alexbartlow/370438 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'socket'
require 'tmpdir'
module Enumerable
def pmap(&block)
sockname = Dir::tmpdir + "ruby_pmap_#{$$}_#{self.object_id}"
aggregator = []
size = entries.size
serv = UNIXServer.new(sockname)
each do |object|
fork do
client = UNIXSocket.open(sockname)
client.puts Marshal.dump(block.call(object))
client.close
end
end
until aggregator.size == size
sock = begin
serv.accept_nonblock
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
IO.select([serv])
retry
end
aggregator << Marshal.load(sock.read)
sock.close
end
aggregator
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment