How can I (simulate?) do asynchronous HTTP requests in plain Ruby, no Rails? -
as learning exercise, making simple ruby command-line application logs handful of websites have public api (reddit, twitter, etc), checks if have new messages, , reads them out me.
it works well... incredibly slowly, because waits each login-getcookies-requestmessages-getmessages cycle complete beyond moving onto next one. love have work asynchronously, can fire off multiple requests simultaneously, deal whichever data comes first, whichever data comes second, etc.
i've googled problem , looked @ other stackoverflow threads, i'm confused different options available, , solutions seem assuming program part of larger rails app, isn't. thought i'd ask: simplest, smartest, efficient way i'm talking about? don't need guided through it, i'd input on situation people know better do, , suggestions should research solve problem.
i'd willing write in javascript run on node if that'd more appropriate.
you should try looking eventmachine. decent implementation of asynchronous http request javascript ajax call em-synchrony
require "em-synchrony" require "em-synchrony/em-http" em.synchrony concurrency = 2 urls = ['http://url.1.com', 'http://url2.com'] em::synchrony::iterator.new(urls, concurrency).each |url| resp = eventmachine::httprequest.new(url).get resp.callback { puts "success callback" } resp.errback { puts "error callback" } puts resp.response end eventmachine.stop end
Comments
Post a Comment