3

我有一个用例,当调用控制器的操作时,它会触发 3 个对其他 url 的 http 请求。我需要解析这些请求的响应,将它们组合起来,然后渲染视图。如何并行而不是顺序触发 3 个请求以节省时间?

4

1 回答 1

2

我使用Typhoes来处理并行调用。

跳转到进行并行调用部分,了解如何进行并行调用。

例子

 # Assume these were your two HTTP calls
 first_request = Typhoes::Request.new('http://jsonplaceholder.typicode.com/posts?userId=2')
 second_request = Typhoes::Request.new('http://jsonplaceholder.typicode.com/posts?userId=3')

 # Initialize a Hydra queue (this holds typhoes requests)
 hydra = Typhoeus::Hydra.hydra
 hydra.queue [first_request, second_request]
 hydra.run # this triggers the calls

 # Getting response, once the run is complete

 first_request.response 
 second_request.response 
于 2015-08-03T14:25:27.037 回答