From: Daniel DeLorme Date: 2010-05-19T10:56:13+09:00 Subject: Re: Asynchronous HTTP request Daniel N wrote: > On 18 May 2010 16:56, Daniel DeLorme wrote: > >> Daniel N wrote: >> >>> On 13 May 2010 17:37, Daniel DeLorme wrote: >>> >>> Does anyone know how to do the following, but without threads, purely >>>> with >>>> asynchronous IO? >>>> >>>> website = Thread.new{ Net::HTTP.get(URI.parse(url)) } >>>> template = compute_lots_of_stuff() >>>> puts template.sub("", website.value) >>>> >>> Event machine is perfect for this kind of stuff. Weather it fits with the >>> rest of your web framework is more likely the thing that makes it an >>> unlikely selection (if you're using anything rack based for example) >>> >> If you could show me how to use EventMachine in this case I'd be grateful. >> I couldn't figure out how to run compute_lots_of_stuff() while the http >> requests are executing. >> > > What context are you trying to do this in? Is it inside a rack request > (rails / merb / sinatra /pancake / other)? or is this in a stand alone > script? > > Could you perhaps provide a bit of context for what you're trying to achive? Ok, here's the context. I didn't put this in my OP because I figured it would just bore everybody to tears. This is inside a rack request. The idea is that I'm assembling a web page by doing a bunch of sub-requests for the various parts of the page. So I'll have something like: action "index" do @news = subreq("http://news.server") @ad = subreq("http://ad.server") @blog = subreq("http://blog.server") @forum = subreq("http://forum.server") end All these sub-requests are launched asynchronously and, while they are executing, the app generates the layout within which the output of the subrequests will be embedded. So I'll have something like: response = ['', '
',@ad,'
', '
',@news,'
', '
',@blog,'
', '
',@forum,'
', ''] And when rack finally outputs the response to the client it will block on the various subrequests unless/until they have completed. What I can't figure out with EventMachine is how to have the "main thread" generate the layout while the subrequests are executing.