Sha256: a5c28a3875c921bd91581b289c05e9fc4770c8e4b8ce262670ee5300a499784a

Contents?: true

Size: 1.38 KB

Versions: 33

Compression:

Stored size: 1.38 KB

Contents

module EventMachine

  # EventMachine based Multi request client, based on a streaming HTTPRequest class, 
  # which allows you to open multiple parallel connections and return only when all
  # of them finish. (i.e. ideal for parallelizing workloads)
  # 
  # == Example
  # 
  #  EventMachine.run {
  #   
  #   multi = EventMachine::MultiRequest.new
  #    
  #   # add multiple requests to the multi-handler
  #   multi.add(EventMachine::HttpRequest.new('http://www.google.com/').get)
  #   multi.add(EventMachine::HttpRequest.new('http://www.yahoo.com/').get)
  #    
  #    multi.callback  {
  #       p multi.responses[:succeeded]
  #       p multi.responses[:failed]
  #     
  #       EventMachine.stop
  #    }
  #  }
  # 
  
  class MultiRequest
    include EventMachine::Deferrable

    attr_reader :requests, :responses
    
    def initialize
      @requests = []
      @responses = {:succeeded => [], :failed => []}
    end
    
    def add(conn)
      conn.callback { @responses[:succeeded].push(conn); check_progress }
      conn.errback  { @responses[:failed].push(conn); check_progress }
      
      @requests.push(conn)
    end
    
    protected
   
    # invoke callback if all requests have completed
    def check_progress
      succeed if (@responses[:succeeded].size + @responses[:failed].size) == @requests.size
    end
    
  end
end

Version data entries

33 entries across 33 versions & 7 rubygems

Version Path
astro-em-http-request-0.1.3.20090419 lib/em-http/multi.rb
astro-em-http-request-0.1.5 lib/em-http/multi.rb
astro-em-http-request-0.1.6 lib/em-http/multi.rb
camerontaylor-em-http-request-0.1.7 lib/em-http/multi.rb
dwaite-em-http-request-0.1.10 lib/em-http/multi.rb
dwaite-em-http-request-0.1.9 lib/em-http/multi.rb
igrigorik-em-http-request-0.1.1 lib/em-http/multi.rb
igrigorik-em-http-request-0.1.3 lib/em-http/multi.rb
igrigorik-em-http-request-0.1.5 lib/em-http/multi.rb
igrigorik-em-http-request-0.1.6 lib/em-http/multi.rb
igrigorik-em-http-request-0.1.7 lib/em-http/multi.rb
julien51-em-http-request-0.1.10 lib/em-http/multi.rb
julien51-em-http-request-0.1.11 lib/em-http/multi.rb
julien51-em-http-request-0.1.9 lib/em-http/multi.rb
tmm1-em-http-request-0.1.0 lib/em-http/multi.rb
tmm1-em-http-request-0.1.1 lib/em-http/multi.rb
igrigorik-em-http-request-0.1.8 lib/em-http/multi.rb
astro-em-http-request-0.2.8 lib/em-http/multi.rb
astro-em-http-request-0.2.7 lib/em-http/multi.rb
astro-em-http-request-0.2.6 lib/em-http/multi.rb