Is this a correct way of dealing with Celluloid?

42 views
Skip to first unread message

bstrom

unread,
Mar 2, 2017, 7:59:03 PM3/2/17
to Celluloid
require 'celluloid/current'
require 'benchmark'

class Integer
 
def fib
   
if self <= 1; self else ( self-1 ).fib  + ( self-2 ).fib end
 
end
end

class FibWorker
  include
Celluloid
 
def do_fib i
    i
.fib
 
end
end

def fib_actor
  pool
= FibWorker.pool size: 8
  fibs
= []
 
38.times do | i | fibs << pool.future.do_fib(i) end
 
38.times do | i | fibs << pool.future.do_fib(i) end
 
38.times do | i | fibs << pool.future.do_fib(i) end
 
38.times do | i | fibs << pool.future.do_fib(i) end
  fibs
.each do |x| puts x.value end
end

def fib_no_actor
 
38.times do | i | puts i.fib end
 
38.times do | i | puts i.fib end
 
38.times do | i | puts i.fib end
 
38.times do | i | puts i.fib end
end

puts
Benchmark.measure { fib_no_actor }
puts
Benchmark.measure { fib_actor }
I'm wondering if this is the "correct" way of using celluloid if you want to repeat something several times in async? Is there any better way than storing each Future in an array and then call the value to wait for each to finish?
Reply all
Reply to author
Forward
0 new messages