Class: EventMachine::AblyHttpRequest::MultiRequest
- Inherits:
-
Object
- Object
- EventMachine::AblyHttpRequest::MultiRequest
- 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::AblyHttpRequest::MultiRequest.new
# add multiple requests to the multi-handler
multi.add(:a, EventMachine::AblyHttpRequest::HttpRequest.new('http://www.google.com/').get)
multi.add(:b, EventMachine::AblyHttpRequest::HttpRequest.new('http://www.yahoo.com/').get)
multi.callback {
p multi.responses[:callback]
p multi.responses[:errback]
EventMachine.stop
}
}
Instance Attribute Summary collapse
-
#requests ⇒ Object
readonly
Returns the value of attribute requests.
-
#responses ⇒ Object
readonly
Returns the value of attribute responses.
Instance Method Summary collapse
- #add(name, conn) ⇒ Object
- #finished? ⇒ Boolean
-
#initialize ⇒ MultiRequest
constructor
A new instance of MultiRequest.
Constructor Details
#initialize ⇒ MultiRequest
Returns a new instance of MultiRequest.
32 33 34 35 |
# File 'lib/em-http/multi.rb', line 32 def initialize @requests = {} @responses = {:callback => {}, :errback => {}} end |
Instance Attribute Details
#requests ⇒ Object (readonly)
Returns the value of attribute requests.
30 31 32 |
# File 'lib/em-http/multi.rb', line 30 def requests @requests end |
#responses ⇒ Object (readonly)
Returns the value of attribute responses.
30 31 32 |
# File 'lib/em-http/multi.rb', line 30 def responses @responses end |
Instance Method Details
#add(name, conn) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/em-http/multi.rb', line 37 def add(name, conn) raise 'Duplicate Multi key' if @requests.key? name @requests[name] = conn conn.callback { @responses[:callback][name] = conn; check_progress } conn.errback { @responses[:errback][name] = conn; check_progress } end |
#finished? ⇒ Boolean
46 47 48 |
# File 'lib/em-http/multi.rb', line 46 def finished? (@responses[:callback].size + @responses[:errback].size) == @requests.size end |