Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created May 16, 2013 08:41
Show Gist options
  • Save judofyr/ba534683a8d2834c783b to your computer and use it in GitHub Desktop.
Save judofyr/ba534683a8d2834c783b to your computer and use it in GitHub Desktop.
require 'celluloid'
class PMapper
include Celluloid
def run(mailbox, i, elem, block)
mailbox << [i, block.call(elem)]
end
end
module Enumerable
def pmap(&block)
futures = map { |elem| Celluloid::Future.new(elem, &block) }
futures.map { |future| future.value }
end
def pmap(workers = 16, &block)
pool = PMapper.pool(:size => workers)
mb = Celluloid::Mailbox.new
i = 0
each do |elem|
pool.async.run(mb, i, elem, block)
i += 1
end
res = []
i.times do
mb.receive { |(idx, value)| res[idx] = value }
end
res
end
end
t = Time.now
p (1..32).pmap { |x| sleep 2; x * 2 }
p Time.now - t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment