Created
February 12, 2012 23:47
-
-
Save enriclluelles/1811883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'eventmachine' | |
require 'em-http' | |
require 'fiber' | |
def async_fetch(url) | |
f = Fiber.current | |
http = EventMachine::HttpRequest.new(url).get | |
http.callback { f.resume(http) } | |
return Fiber.yield | |
end | |
numbers = (1..25).to_a | |
t1 = Time.now | |
counter = 0 | |
EventMachine.run do | |
numbers.each do |number| | |
Fiber.new{ | |
data = async_fetch("http://www.google.com/search?q=#{number}") | |
counter += 1 | |
EventMachine.stop if counter == numbers.size | |
}.resume | |
end | |
end | |
puts (Time.now - t1).to_s | |
t2 = Time.now | |
counter = 0 | |
EventMachine.run do | |
numbers.each do |number| | |
http = EventMachine::HttpRequest.new("http://www.google.com/search?q=#{number}").get | |
http.callback do |data| | |
counter += 1 | |
EventMachine.stop if counter == numbers.size | |
end | |
end | |
end | |
puts (Time.now - t2).to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment