Skip to content

Instantly share code, notes, and snippets.

@NateBarnes
Last active December 16, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NateBarnes/5401122 to your computer and use it in GitHub Desktop.
Save NateBarnes/5401122 to your computer and use it in GitHub Desktop.
Just a quick example of parallelizing network requests to improve SoA API call times to multiple endpoints using future methods.
require "httparty"
require "celluloid"
start_time = Time.now
results = []
100.times do
results << HTTParty.get("http://www.reddit.com/r/ruby")
end
end_time = Time.now
puts "Procedural: #{end_time-start_time}"
start_time = Time.now
methods = []
results = []
100.times do
methods << Celluloid::Future.new { HTTParty.get("http://www.reddit.com/r/ruby") }
end
methods.each do |meth|
results << meth.value
end
end_time = Time.now
puts "Parallel: #{end_time-start_time}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment