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}
Written by Jeremy Raines
Related protips
1 Response
Note that this code is running on top of JRuby
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#