Json EM-HTTP-REQUEST和SINATRA-将多个api请求合并为一个结果?

Json EM-HTTP-REQUEST和SINATRA-将多个api请求合并为一个结果?,json,sinatra,em-http-request,Json,Sinatra,Em Http Request,我第一次处理sinatra和并行em http请求。我不知道如何组合/合并成一个结果,以及何时使用EventMachine.stop。考虑这一点: get '/data/:query' do content_type :json EventMachine.run do http1 = EventMachine::HttpRequest.new('v1/').get http2 = EventMachine::HttpRequest.new('v2/').get

我第一次处理sinatra和并行em http请求。我不知道如何组合/合并成一个结果,以及何时使用EventMachine.stop。考虑这一点:

get '/data/:query' do
  content_type :json

  EventMachine.run do
    http1 = EventMachine::HttpRequest.new('v1/').get
    http2 = EventMachine::HttpRequest.new('v2/').get

    http1.errback { p 'Uh oh nooooooo'; EventMachine.stop }

    http1.callback {
     // do some operation http1.repsonse
     Crack::XML.parse(http1.response).to_json
      EventMachine.stop
    }

    http2.callback {
     // do some operation http2.response
     Crack::XML.parse(http2.response).to_json
     EventMachine.stop
    } 

  end

  somehow merge 
  return merged_result

end

上面的例子有一个竞态条件——当一个请求完成时,您将停止eventloop。要解决此问题,您可以使用内置的“多”接口:

EventMachine.run do
  multi = EventMachine::MultiRequest.new

  multi.add :google, EventMachine::HttpRequest.new('http://www.google.com/').get
  multi.add :yahoo, EventMachine::HttpRequest.new('http://www.yahoo.com/').get

  multi.callback do
    puts multi.responses[:callback]
    puts multi.responses[:errback]
    EventMachine.stop
  end
end

有关更多信息,请参见em http wiki页面:

上面的示例有一个竞争条件-一旦其中一个请求完成,您将停止eventloop。要解决此问题,您可以使用内置的“多”接口:

EventMachine.run do
  multi = EventMachine::MultiRequest.new

  multi.add :google, EventMachine::HttpRequest.new('http://www.google.com/').get
  multi.add :yahoo, EventMachine::HttpRequest.new('http://www.yahoo.com/').get

  multi.callback do
    puts multi.responses[:callback]
    puts multi.responses[:errback]
    EventMachine.stop
  end
end

有关更多信息,请参见em http wiki页面:

上面的示例有一个竞争条件-一旦其中一个请求完成,您将停止eventloop。要解决此问题,您可以使用内置的“多”接口:

EventMachine.run do
  multi = EventMachine::MultiRequest.new

  multi.add :google, EventMachine::HttpRequest.new('http://www.google.com/').get
  multi.add :yahoo, EventMachine::HttpRequest.new('http://www.yahoo.com/').get

  multi.callback do
    puts multi.responses[:callback]
    puts multi.responses[:errback]
    EventMachine.stop
  end
end

有关更多信息,请参见em http wiki页面:

上面的示例有一个竞争条件-一旦其中一个请求完成,您将停止eventloop。要解决此问题,您可以使用内置的“多”接口:

EventMachine.run do
  multi = EventMachine::MultiRequest.new

  multi.add :google, EventMachine::HttpRequest.new('http://www.google.com/').get
  multi.add :yahoo, EventMachine::HttpRequest.new('http://www.yahoo.com/').get

  multi.callback do
    puts multi.responses[:callback]
    puts multi.responses[:errback]
    EventMachine.stop
  end
end

有关更多信息,请参见em http wiki页面:

谢谢您回到我身边。我可以请你更详细和耐心一点吗?如何分别解析和响应google和yahooo,并对它们执行不同的操作?之后将它们合并,并将此合并产品作为sinatra的回复返回?谢谢您回到我这里。我可以请你更详细和耐心一点吗?如何分别解析和响应google和yahooo,并对它们执行不同的操作?之后将它们合并,并将此合并产品作为sinatra的回复返回?谢谢您回到我这里。我可以请你更详细和耐心一点吗?如何分别解析和响应google和yahooo,并对它们执行不同的操作?之后将它们合并,并将此合并产品作为sinatra的回复返回?谢谢您回到我这里。我可以请你更详细和耐心一点吗?如何分别解析和响应google和yahooo,并对它们执行不同的操作?在之后合并它们,并将此合并产品作为sinatra的响应返回?