在 Ruby 中执行非阻塞 I/O 的首选方式是什么?

发布于 2024-10-08 05:44:47 字数 67 浏览 3 评论 0原文

如果说我想检索一个网页进行解析,但在 I/O 发生时不阻塞 CPU。有没有相当于Python的Eventlet库的东西?

If say I want to retrieve a web page for parsing, but not block the CPU while the I/O is taking place. Is there something equivalent to Python's Eventlet library?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2024-10-15 05:44:47

Ruby 最好的 HTTP 客户端库是 Typhhoeus,它可用于并行执行多个 HTTP 请求非阻塞时尚。有阻塞和非阻塞接口:

# blocking
response = Typhoeus::Request.get("http://stackoverflow.com/")
puts response.body

# non-blocking
request1 = Typhoeus::Request.new("http://stackoverflow.com/")
request1.on_complete do |response|
  puts response.body
end
request2 = Typhoeus::Request.new("http://stackoverflow.com/questions")
request2.on_complete do |response|
  puts response.body
end
hydra = Typhoeus::Hydra.new
hydra.queue(request1)
hydra.queue(request2)
hydra.run # this call is blocking, though

另一个选项是 em-http-request,它运行在 EventMachine 之上。它有一个完全非阻塞的接口:

EventMachine.run do
  request = EventMachine::HttpRequest.new('http://stackoverflow.com/').get
  request.callback do
    puts request.response
    EventMachine.stop
  end
end

还有一个用于并行发出许多请求的接口,类似于 Typhoeus Hydra。

em-http-request 的缺点是它与 EventMachine 绑定。 EventMachine 本身就是一个很棒的框架,但它是一个要么全有要么全无的交易。您需要以事件/连续传递风格的方式编写整个应用程序,众所周知,这会导致大脑损伤。 Typhoeus 更适合尚未发生事件的应用程序。

The best HTTP client library for Ruby is Typhoeus, it can be used to perform multiple HTTP requests in parallel in a non-blocking fashion. There is a blocking and non-blocking interface:

# blocking
response = Typhoeus::Request.get("http://stackoverflow.com/")
puts response.body

# non-blocking
request1 = Typhoeus::Request.new("http://stackoverflow.com/")
request1.on_complete do |response|
  puts response.body
end
request2 = Typhoeus::Request.new("http://stackoverflow.com/questions")
request2.on_complete do |response|
  puts response.body
end
hydra = Typhoeus::Hydra.new
hydra.queue(request1)
hydra.queue(request2)
hydra.run # this call is blocking, though

Another option is em-http-request, which runs on top of EventMachine. It has a completely non-blocking interface:

EventMachine.run do
  request = EventMachine::HttpRequest.new('http://stackoverflow.com/').get
  request.callback do
    puts request.response
    EventMachine.stop
  end
end

There's also an interface for making many requests in parallel, similarly to Typhoeus Hydra.

The downside of em-http-request is that it is tied to EventMachine. EventMachine is an awesome framework in itself, but it's an all-or-nothing deal. You need to write your whole application in an evented/continuation-passing-style fashion, and that has been known to cause brain damage. Typhoeus is much better suited to applications that are not already evented.

心碎的声音 2024-10-15 05:44:47

我不确定 Eventlet 是做什么的,但是 Ruby 有 EventMachine,一个用于非阻塞 IO 的库 (除其他外)。

I'm not sure what Eventlet does, but Ruby has EventMachine, a library for non-blocking IO (amongst other things).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文