如何在 Ruby 中使用 Eventmachine 并行获取多个 JSON
我是 EM 新手,正在遵循这个示例:
EventMachine.run {
http = EventMachine::HttpRequest.new('http://google.com/').get :query => {'keyname' => 'value'}
http.errback { p 'Uh oh'; EM.stop }
http.callback {
p http.response_header.status
p http.response_header
p http.response
EventMachine.stop
}
}
我想做类似的事情。
我想从多个不同的 Web 服务器并行获取“JavaScript 对象表示法”(JSON) 文件。
我找不到如何将所有这些 JSON 文件存储在公共变量中的方法,以便之后我可以对它们进行一些计算,就像在每个请求中我将 JSON 存储在全局数组中一样。
I'm new to EM and am following this example:
EventMachine.run {
http = EventMachine::HttpRequest.new('http://google.com/').get :query => {'keyname' => 'value'}
http.errback { p 'Uh oh'; EM.stop }
http.callback {
p http.response_header.status
p http.response_header
p http.response
EventMachine.stop
}
}
I want to do something similar.
I want to fetch "JavaScript Object Notation" (JSON) files from several different web servers, in parallel.
I cannot find the way how to store all these JSON files in a common variable, so that I can do some calculations about them afterwards, something like in every request I store the JSON in a global array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望请求并行并在所有请求完成后处理它们吗?
您可以使用 em-http-request 中的
EventMachine::MultiRequest
一个>。 wiki 有关于发出并行请求的文档,请参阅“与多接口同步”。您应该将我们的代码添加到
multi.callback
中,您将收到一组请求。You want the requests to be in parallel and to process them after all have been completed?
You can use
EventMachine::MultiRequest
from em-http-request. The wiki has documentation on issuing parallel requests, see "Synchronizing with Multi interface".You should add our code to
multi.callback
and you will receive an array of requests.