红宝石异步HTTP请求 [英] Asynchronous HTTP requests with ruby

查看:103
本文介绍了红宝石异步HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RabbitMQ的队列满的请求,我想送请求作为HTTP GET异步,而无需等待每个请求的响应。现在我感到困惑的是什么是更好地使用,螺纹或只是EM?我使用它目前的方式是类似于以下,但它会是巨大的,知道是否有在这里更好的性能,因为它是该计划的一个非常关键的部分没有更好的实现:

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

通过上面的code,是系统会等待为每个请求启动下一个前完成?如果这里有任何提示我会强烈AP preciate它

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

推荐答案

HTTP是同步的,所以你必须等待回复。如果要模拟一个异步环境,你可以有一个线程池,并通过每个请求一个线程将等待您的答复,然后返回池中,直到下一个请求。你要么发送线程的回调函数,当回答完,还是会立即返回未来的回复对象,它可以让你推迟等待答复,直到你真正需要的回复数据使用。

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.

这篇关于红宝石异步HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆