Sha256: 9e4d1262c92404f82f8d77f62107d899a41b3647347d2db501a09ef2c1c34a4e

Contents?: true

Size: 864 Bytes

Versions: 20

Compression:

Stored size: 864 Bytes

Contents

require 'eventmachine'
require 'em-http'
require 'fiber'

# Using Fibers in Ruby 1.9 to simulate blocking IO / IO scheduling
# while using the async EventMachine API's

def async_fetch(url)
  f = Fiber.current
  http = EventMachine::HttpRequest.new(url).get :timeout => 10

  http.callback { f.resume(http) }
  http.errback  { f.resume(http) }

  return Fiber.yield
end

EventMachine.run do
  Fiber.new{

    puts "Setting up HTTP request #1"
    data = async_fetch('http://www.google.com/')
    puts "Fetched page #1: #{data.response_header.status}"

    puts "Setting up HTTP request #2"
    data = async_fetch('http://www.yahoo.com/')
    puts "Fetched page #2: #{data.response_header.status}"

    EventMachine.stop
  }.resume
end

puts "Done"

#  Setting up HTTP request #1
#  Fetched page #1: 302
#  Setting up HTTP request #2
#  Fetched page #2: 200
#  Done

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
em-http-request-1.0.0.beta.3 examples/fibered-http.rb
em-http-request-1.0.0.beta.2 examples/fibered-http.rb
em-http-request-1.0.0.beta.1 examples/fibered-http.rb
em-http-request-0.2.10 examples/fibered-http.rb
em-http-request-0.2.9 examples/fibered-http.rb
astro-em-http-request-0.2.10 examples/fibered-http.rb
astro-em-http-request-0.2.9 examples/fibered-http.rb
astro-em-http-request-0.2.8 examples/fibered-http.rb
astro-em-http-request-0.2.7 examples/fibered-http.rb
em-http-request-0.2.7 examples/fibered-http.rb
astro-em-http-request-0.2.6 examples/fibered-http.rb
em-http-request-0.2.6 examples/fibered-http.rb
em-http-request-0.2.5 examples/fibered-http.rb
em-http-request-0.2.4 examples/fibered-http.rb
em-http-request-0.2.3 examples/fibered-http.rb
astro-em-http-request-0.2.3 examples/fibered-http.rb
astro-em-http-request-0.2.2 examples/fibered-http.rb
em-http-request-0.2.2 examples/fibered-http.rb
em-http-request-0.2.1 examples/fibered-http.rb
em-http-request-0.2.0 examples/fibered-http.rb