Skip to content

Instantly share code, notes, and snippets.

@artofhuman
Created April 16, 2013 05:22
Show Gist options
  • Save artofhuman/5393566 to your computer and use it in GitHub Desktop.
Save artofhuman/5393566 to your computer and use it in GitHub Desktop.
pmap
# coding utf-8
class Array
def pmap
pipes, out = [], []
each do |e|
r, w = IO.pipe
Process.fork do
r.close
res = yield(e)
w.write(Marshal.dump(res))
end
w.close
pipes << r
end
Process.waitall
pipes.each do |p|
out << Marshal.load(p.read)
p.close
end
out
end # end pmap
end # end Array
require 'benchmark'
seconds = Benchmark.realtime do
test = [1, 2, 3].pmap do |x|
sleep x
x**x
end
p test
end
puts "work #{seconds} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment