asynchronous http request in ruby

后端 未结 3 1607
情深已故
情深已故 2020-12-05 17:51
 require \'net/http\'

urls = [
  {\'link\' => \'http://www.google.com/\'},
  {\'link\' => \'http://www.facebook.com/\'},
 {\'link\' => \'http://www.yahoo.c         


        
相关标签:
3条回答
  • 2020-12-05 18:00

    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.

    0 讨论(0)
  • 2020-12-05 18:00

    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 code
    response.headers # Response headers
    response.body # Parsed body
    response.raw_body # Unparsed body
    }
    
    0 讨论(0)
  • 2020-12-05 18:14

    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.

    0 讨论(0)
提交回复
热议问题