在 Ruby 中执行非阻塞 I/O 的首选方式是什么?
如果说我想检索一个网页进行解析,但在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ruby 最好的 HTTP 客户端库是 Typhhoeus,它可用于并行执行多个 HTTP 请求非阻塞时尚。有阻塞和非阻塞接口:
另一个选项是 em-http-request,它运行在 EventMachine 之上。它有一个完全非阻塞的接口:
还有一个用于并行发出许多请求的接口,类似于 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:
Another option is em-http-request, which runs on top of EventMachine. It has a completely non-blocking interface:
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.
我不确定 Eventlet 是做什么的,但是 Ruby 有 EventMachine,一个用于非阻塞 IO 的库 (除其他外)。
I'm not sure what Eventlet does, but Ruby has EventMachine, a library for non-blocking IO (amongst other things).