Skip to content

Instantly share code, notes, and snippets.

@enriclluelles
Created February 12, 2012 23:47
Show Gist options
  • Save enriclluelles/1811883 to your computer and use it in GitHub Desktop.
Save enriclluelles/1811883 to your computer and use it in GitHub Desktop.
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