From: "Ara.T.Howard" Date: 2003-10-04T15:13:28+09:00 Subject: Re: webrick, threads, and i/o On Sat, 4 Oct 2003, NAKAMURA, Hiroshi wrote: > Hi, > > > From: "Ara.T.Howard" > > Sent: Saturday, October 04, 2003 11:12 AM > > > if i want to write a webrick application that mounts many servlets do i need > > to be concerned that, if one of the servlets blocks on i/o, the entire > > (threaded) webserver will also be blocked? > > No. WEBrick creates worker thread for each request. > So, take care if you mount a servlet factory which returns > singleton servlet instance. It runs under MT condition. > > Regards, > // NaHi my earlier post could have used an example, here is an example of where blocking in one request seems to block the entire server: ----CUT---- #!/usr/local/ruby-1.8.0/bin/ruby require 'webrick' include WEBrick system 'mkfifo fifo' rescue nil port = (ARGV[0] or 2003).to_i s = HTTPServer.new( :Port => port ) #res.body = "hello, world." #res['Content-Type'] = "text/html" class TimeServlet < HTTPServlet::AbstractServlet def do_GET(req, res) res['Content-Type'] = "text/plain" res.body = Time.now.to_s end end class ReadServlet < HTTPServlet::AbstractServlet def do_GET(req, res) res['Content-Type'] = "text/plain" fifo = open 'fifo', 'r' res.body = fifo.read end end class WriteServlet < HTTPServlet::AbstractServlet def do_GET(req, res) res['Content-Type'] = "text/plain" fifo = open 'fifo', 'w' msg = "%s:%s" % [self.class, Time.now.to_s] fifo.puts msg res.body = "wrote <%s>" % [msg] end end s.mount("/write", WriteServlet) s.mount("/read", ReadServlet) s.mount("/time", TimeServlet) trap("INT"){ s.shutdown } s.start ----CUT---- now, if you run this and then point your browser at http://127.0.0.1:2003/time you will see the time. however, if you then try one, or more, of the following in any order http://127.0.0.1:2003/read http://127.0.0.1:2003/write and return to http://127.0.0.1:2003/time you will see ALL windows hanging. does this make sense? how does one go about designing servlets which will then block, like POST servlets, in a way that would not block the entire webserver? thanks. -a ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 | The difference between art and science is that science is what we understand | well enough to explain to a computer. Art is everything else. | -- Donald Knuth, "Discover" | ~ > /bin/sh -c 'for lang in ruby perl; do $lang -e "print \"\x3a\x2d\x29\x0a\""; done' ====================================