Sha256: f98173c8d7e58b40899f48073b573c10d2a41706f7d17fb025c9ca04887a4af6

Contents?: true

Size: 902 Bytes

Versions: 6

Compression:

Stored size: 902 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

6 entries across 6 versions & 1 rubygems

Version Path
em-http-request-0.3.0 examples/fibered-http.rb
em-http-request-0.2.15 examples/fibered-http.rb
em-http-request-0.2.14 examples/fibered-http.rb
em-http-request-0.2.13 examples/fibered-http.rb
em-http-request-0.2.12 examples/fibered-http.rb
em-http-request-0.2.11 examples/fibered-http.rb