From: Daniel DeLorme Date: 2010-05-13T22:29:02+09:00 Subject: Re: Asynchronous HTTP request Brian Candler wrote: > 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) > > Depends what you mean by "with asynchronous IO". Do you want to keep > calling select() and then only read data when its available? Then you're > basically rewriting eventmachine or io-reactor. I mean nonblocking. I don't want to keep calling select(), I just want to call it once, when I'm ready to process the data I asked for. > But if you don't want the code to block until the body has read, but you > don't want the read to take place in another thread, then what do you > want? I just want to issue the http request, do other stuff while the request goes on its merry way, let the response accumulate at the socket, and read the data when I'm ready to. If at that time the response has accumulated at the socket then I don't have to wait, otherwise block until the data has finished coming in. > What's the problem with threads anyway? Being able to do one thing while > you're waiting for something else to complete is exactly what they're > for. I can't agree with that. Thread are meant to achieve *concurrency*, meaning the concurrent (or at least interleaved) execution of instructions. If the only thing the IO thread does is wait for the data and then exit, there's nothing concurrent happening; it's just a way to simulate nonblocking IO. And creating a thread just for that seems to me like the proverbial jackhammer to drive in a nail, especially since 1.9 threads are no longer green. Given that nonblocking IO is a paradigm that's been around for ages I was kinda hoping there was a neat & tidy way of doing it (a gem maybe?) but I haven't found it.