Ruby on rails 对于相同的请求,em同步不是异步的
在rails应用程序中,我有一个异步方法,它只在请求不同时异步工作 在我的控制器中,我有以下方法:Ruby on rails 对于相同的请求,em同步不是异步的,ruby-on-rails,ruby,asynchronous,thin,eventmachine,Ruby On Rails,Ruby,Asynchronous,Thin,Eventmachine,在rails应用程序中,我有一个异步方法,它只在请求不同时异步工作 在我的控制器中,我有以下方法: require "em-synchrony/em-http" def test EventMachine.synchrony do page = EventMachine::HttpRequest.new("http://127.0.0.1:8081/").get render :json => {result: page.response}
require "em-synchrony/em-http"
def test
EventMachine.synchrony do
page = EventMachine::HttpRequest.new("http://127.0.0.1:8081/").get
render :json => {result: page.response}
request.env['async.callback'].call(response)
end
throw :async
end
在我的页面中,我这样调用此方法:
//Not asynchronous. :(
//The second request takes twice more time than the first one
$.get("/test");
$.get("/test");
但是,要使调用异步,我需要请求不同,如下所示:
//Asynchronous. :D
$.get("/test?a");
$.get("/test?b");
为什么??
我希望我的代码总是异步的。即使是相同的请求。仅供参考,我正在使用服务器瘦我发现您的问题非常有趣,因为我将实现我的第一个基于Reactor模式的web服务器,当然我还通过了em syncrony 您是否也尝试过使用
aget
而不是get
page = EventMachine::HttpRequest.new("http://127.0.0.1:8081/").aget
如果有什么不同,请告诉我:)