Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initiate multiple parallel http requests using EventMachine with Ruby 1.8

em-synchrony.rb implements this feature with Fibers but I would go for a non-Fibre version with 1.8 MRI.

EM.run do
  http = EM::Protocols::HttpClient2.connect("www.google.com", 80)
  request = http.get("/")
  request.callback do
    puts request.status
    EM.stop
  end
end
like image 442
Istvan Avatar asked Dec 14 '25 10:12

Istvan


1 Answers

Have a look at em-http-request:

EM.run do
  http1 = EventMachine::HttpRequest.new('http://example.com/1').get
  http1.callback do
    p http1.response
  end
  http2 = EventMachine::HttpRequest.new('http://example.com/2').get
  http2.callback do
    p http2.response
  end
end
like image 170
Theo Avatar answered Dec 17 '25 02:12

Theo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!