From: Ben Giddings Date: 2009-09-11T05:48:47+09:00 Subject: Re: Asynchronous http POST? On Thursday 10 September 2009 15:48:56 Ivan Shevanski wrote: > Apparently, since control is not returned to the interpreter, when one > thread waits the other(s) will not continue. At least that's my > understanding. A quick test seems to show that isn't the case. I wrote a simple webrick servlet that accepts a post request and delays for a specified amount of time (from the delay parameter to the post), and a client with 2 threads that post to those URLs and keep track of when things start and end: delay_servlet.rb: require 'webrick' require 'time' class DelayServlet < WEBrick::HTTPServlet::AbstractServlet def do_POST(request, response) start_time = Time.now delay = 0 if request.query["delay"] delay = request.query["delay"].to_i end sleep(delay) end_time = Time.now response.body = "delayed for #{delay}s, started at " + "#{start_time.iso8601}, ended at #{end_time.iso8601}\n" end end if __FILE__ == $0 server = WEBrick::HTTPServer.new(:Port => 8000) server.mount("/", DelayServlet) trap("INT") {server.shutdown} server.start end delay_client.rb: require 'net/http' require 'time' if __FILE__ == $0 puts "Main thread start at #{Time.now.iso8601}" t1 = Thread.new do puts "Thread 1 start at #{Time.now.iso8601}" res = Net::HTTP.post_form(URI.parse('http://localhost:8000/'), {'delay'=>'5'}) puts "Response: " + res.body puts "Thread 1 end at #{Time.now.iso8601}" end t2 = Thread.new do puts "Thread 2 start at #{Time.now.iso8601}" res = Net::HTTP.post_form(URI.parse('http://localhost:8000/'), {'delay'=>'7'}) puts "Response: " + res.body puts "Thread 2 end at #{Time.now.iso8601}" end t1.join t2.join puts "Main thread end at #{Time.now.iso8601}" end Output: Main thread start at 2009-09-10T16:46:17-04:00 Thread 1 start at 2009-09-10T16:46:17-04:00 Thread 2 start at 2009-09-10T16:46:17-04:00 Response: delayed for 5s, started at 2009-09-10T16:46:17-04:00, ended at 2009-09-10T16:46:22-04:00 Thread 1 end at 2009-09-10T16:46:22-04:00 Response: delayed for 7s, started at 2009-09-10T16:46:17-04:00, ended at 2009-09-10T16:46:24-04:00 Thread 2 end at 2009-09-10T16:46:24-04:00 Main thread end at 2009-09-10T16:46:24-04:00 So it sure looks like it isn't blocking all threads when waiting for a HTTP response. Ben