What is the preferred way to do non-blocking I / O in Ruby?

If I say that I want to get a web page for parsing, but not block the processor while I / O occurs. Is there something equivalent to the Python Eventlet library?

+8
ruby nonblocking rubygems
source share
2 answers

The best HTTP client library for Ruby Typhoeus , it can be used to simultaneously execute multiple HTTP requests in a non-blocking mode. 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 fully 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 is also an interface for creating multiple queries in parallel, similar to Typhoeus Hydra.

The disadvantage of an em-http request is that it is tied to EventMachine. EventMachine is a terrific structure in itself, but it's an all-or-nothing deal. You need to write your entire application in the form of an event / continuation of the passage, and this, as you know, causes damage to the brain. Typhoeus is much better suited for applications that are not yet found.

+17
source share

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

+5
source share

Source: https://habr.com/ru/post/650233/


All Articles