Skip to content

Instantly share code, notes, and snippets.

@gotar
Last active December 10, 2015 17:18
Show Gist options
  • Select an option

  • Save gotar/4466939 to your computer and use it in GitHub Desktop.

Select an option

Save gotar/4466939 to your computer and use it in GitHub Desktop.
require 'celluloid'
module Enumerable
# Parallel map. Calculates each element in a separate thread.
def pmap(timeout=nil, &block)
map {|element|
Celluloid::Future.new(element, &block)
}.map{|result|
begin
result.value(timeout)
rescue RuntimeError
end
}.compact
end
end
#for example
[1,2,3,4].pmap{|x| x*x}
[1,2,3,4].pmap(5){|x| x*x} #- the same but with 5 sec limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment