是否可以在Ruby 1.8中使用EventMachine发起多个并行的HTTP请求 [英] Is it possible to initiate multiple parallel http requests using EventMachine with Ruby 1.8

查看:71
本文介绍了是否可以在Ruby 1.8中使用EventMachine发起多个并行的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

em-synchrony.rb在Fibers中实现了此功能,但我会选择1.8 MRI的非Fibre版本.

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

推荐答案

看看 em- http-请求:

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

这篇关于是否可以在Ruby 1.8中使用EventMachine发起多个并行的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆