在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}
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");为什么?我希望我的代码总是异步的。即使对于相同的请求也是如此。仅供参考,我正在使用瘦服务器
发布于 2015-03-10 22:50:25
我发现你的问题真的很有趣,因为我将实现我的第一个基于反应器模式的web服务器,当然,我也经历了em-syncrony。
您是否尝试过使用aget而不是get
page = EventMachine::HttpRequest.new("http://127.0.0.1:8081/").aget如果有什么不同,请让我知道:)!
https://stackoverflow.com/questions/28869367
复制相似问题