Class: EventMachine::MultiRequest

Inherits:
Object
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em-http/multi.rb

Overview

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
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMultiRequest

Returns a new instance of MultiRequest.



31
32
33
34
# File 'lib/em-http/multi.rb', line 31

def initialize
  @requests = []
  @responses = {:succeeded => [], :failed => []}
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



29
30
31
# File 'lib/em-http/multi.rb', line 29

def requests
  @requests
end

#responsesObject (readonly)

Returns the value of attribute responses.



29
30
31
# File 'lib/em-http/multi.rb', line 29

def responses
  @responses
end

Instance Method Details

#add(conn) ⇒ Object



36
37
38
39
40
41
# File 'lib/em-http/multi.rb', line 36

def add(conn)
  conn.callback { @responses[:succeeded].push(conn); check_progress }
  conn.errback  { @responses[:failed].push(conn); check_progress }
  
  @requests.push(conn)
end