asynchronous http request in ruby asynchronous http request in ruby ruby ruby

asynchronous http request in ruby


Lightweight Async handling is the job of Threads (as you said) or Fibers.

Otherwise, you should consider EventMachine which is a very powerful tool.

EDIT: The above URL for Event Machine is dead. Here is their GitHub account, https://github.com/eventmachine/eventmachine . It serves as a good starting point.


Here is a great article covering the topic.

Generally, viable alternatives to using threads for this would be the use of a Fiber or you could use em-http-request. In the latter example you could leave out the callback handling for your particular purpose.


If its just about plain http requests in async style, probably Unirest is the best fit to achieve it.

Asnc request is as simple as:

response = Unirest.post "http://httpbin.org/post",                     headers:{ "Accept" => "application/json" },                     parameters:{ :age => 23, :foo => "bar" } {|response|response.code # Status coderesponse.headers # Response headersresponse.body # Parsed bodyresponse.raw_body # Unparsed body}