如何在 Ruby 中使用 Eventmachine 并行获取多个 JSON

发布于 2024-12-03 20:50:57 字数 493 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还给你自由 2024-12-10 20:50:57

您希望请求并行并在所有请求完成后处理它们吗?

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文