Asynchronous HTTP requests with ruby

徘徊边缘 提交于 2019-12-11 11:29:13

问题


I have a rabbitmq queue full of requests and I want to send the requests as an HTTP GET asynchronously, without the need to wait for each request response. now I'm confused of what is better to use, threads or just EM ? The way i'm using it at the moment is something like the following , but it would be great to know if there is any better implementation with better performance here since it is a very crucial part of the program :

AMQP.start(:host => "localhost") do |connection|
  queue = MQ.queue("some_queue")
  queue.subscribe do |body|
    EventMachine::HttpRequest.new('http://localhost:9292/faye').post :body => {:message =>  body.to_json }
  end
end

With the code above, is the system will wait for each request to finish before starting the next one ? and if there any tips here I would highly appreciate it


回答1:


HTTP is synchronous so you have to wait for the replies. If you want to simulate an async environment that you could have a thread pool and pass each request to a thread which will wait for the reply, then go back in the pool until the next request. You would either send the thread a callback function to use when the reply is finished or you would immediately return a future reply object, which allows you to put off waiting for the reply until you actually need the reply data.

The other way is to have a pool of processes each one of which is processing a request, waiting for the reply, etc.

In both cases, you have to have a pool that is big enough or else you will still end up waiting some of the time.



来源:https://stackoverflow.com/questions/9344282/asynchronous-http-requests-with-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!