Last Updated: February 25, 2016
·
612
· jraines

Concurrent calculations in Ruby, with Celluloid

We recently needed a way to speed up a process for calculating the percent growth for several periods of time (for example, every week of a year). Celluloid -- the Ruby actor library -- and its futures makes this easy

(7..365).step(7).each do |d|
  start_date = d.days.ago
  end_date = (d - 7).days.ago

  calc = GrowthCalculator.new(start_date, end_date)

   #kick off the calculation and continue execution
   growth = calc.future.percent_growth

   results << { name: "Growth %", percent: growth }
end

# "realize" the value of the future
results.each {|r| r[:percent] = r[:percent].value}

1 Response
Add your response

Note that this code is running on top of JRuby

over 1 year ago ·