Two issues with webrick - both threading & OS related

Discussion in 'Ruby' started by rdlugosz.1044583@bloglines.com, Aug 31, 2004.

  1. Guest

    All -

    I've recently been playing with a simple webrick servlet that will
    allow me to build playlists and stream mp3s off of my home system. I've ran
    into some very peculiar issues that appear to be thread & OS related. It
    seems that on Linux, my program will only handle *one* connection at a time;
    i.e. - if your media player is streaming a file, the browser cannot load the
    pages to look at other music directories. However, on WinXP I can have several
    simultaneous connections to the system & there are no blocking issues.

    The media program aside, working with Jim Weirich today we discovered some
    other interesting things. A absolutely basic webrick servlet that has a Thread.sleep
    in it's do_get will block all access to the servlet on any OS - the blocked
    request will execute immediately upon the blocker completing. Here's the
    code:
    class SleepServer < HTTPServlet::AbstractServlet
    def do_GET(req,
    res)
    sleep(30)
    res.body = '<h1>DONE</h1>'
    end

    def do_POST(req,
    res)
    do_GET(req, res)
    end
    end

    This seems pretty strange; it's
    as if webrick is operating in a "Single thread model" servlet mode. I'm sure
    that's not the correct behavior.


    Finally, another issue we ran into is
    one somehow related to threads and IO and is OS specific. The code below
    executes properly on Linux, but appears to deadlock on XP.
    class Streamlet
    < WEBrick::HTTPServlet::AbstractServlet
    def do_GET(req, res)
    res["content-type"]
    = "text/plain"
    reader, writer = IO.pipe
    Thread.start{

    1000.times{|i|
    puts i
    writer << "a" * 1000

    writer << "\n"
    }
    writer.close
    }
    res.body
    = reader
    end
    end

    Personally, I think this problem is even more strange,
    since Ruby is handling the threads internally it doesn't seem like it should
    deadlock on one OS and not the other.

    The issue I'm having with my media
    player is strange too, since it has problems only on Linux yet our test code
    (the sleep(30) servlet) is broken on both platforms...

    Sorry for the length
    of the email, but I felt the detail was required. I appreciate any suggestions
    you may have.

    -Ryan
     
    , Aug 31, 2004
    #1
    1. Advertising

  2. writes:

    > The media program aside, working with Jim Weirich today we discovered some
    > other interesting things. A absolutely basic webrick servlet that has a Thread.sleep
    > in it's do_get will block all access to the servlet on any OS - the blocked
    > request will execute immediately upon the blocker completing. Here's the


    Funny, I do not observe that behaviour:

    --------------------------------------------------
    class SleepServer < HTTPServlet::AbstractServlet
    def do_GET(req,resp)
    resp.body="Start: #{Time.new}\n"
    sleep(10)
    resp.body << "End: #{Time.new}\n"
    resp['content-type'] = 'text/plain'
    end
    end

    class GreetServer < HTTPServlet::AbstractServlet
    def do_GET(req, resp)
    resp.body="Hi, now is: #{Time.new}\n"
    resp['content-type']='text/plain'
    end
    end

    start_webrick {|server|
    server.mount('/sleep', SleepServer)
    server.mount('/greet', GreetServer)
    }
    --------------------------------------------------

    I then open 3 terminals and run:

    Terminal 1:

    dede:~$ w3m -dump http://localhost:8080/sleep
    Start: Tue Aug 31 06:16:35 PDT 2004
    End: Tue Aug 31 06:16:45 PDT 2004

    Terminal 2:

    dede:~$ w3m -dump http://localhost:8080/sleep
    Start: Tue Aug 31 06:16:37 PDT 2004
    End: Tue Aug 31 06:16:47 PDT 2004


    Terminal 3:

    ede:~$ w3m -dump http://localhost:8080/greet
    Hi, now is: Tue Aug 31 06:16:39 PDT 2004
    dede:~$ w3m -dump http://localhost:8080/greet
    Hi, now is: Tue Aug 31 06:16:41 PDT 2004
    dede:~$ w3m -dump http://localhost:8080/greet
    Hi, now is: Tue Aug 31 06:16:43 PDT 2004
    dede:~$ w3m -dump http://localhost:8080/greet
    Hi, now is: Tue Aug 31 06:16:50 PDT 2004
    dede:~$ ruby -v
    ruby 1.8.1 (2004-02-03) [i386-linux]

    Notice that while the /sleep in terminal 1 is waiting, another /sleep
    call on terminal 2 is issued. And while terminals 1 and 2 are waiting
    for the output, multiple /greet calls on terminal 3 can be executed
    just fine.

    So, I do not observe the blocking phenomenon you observed.

    I can't verify your next problem since I don't have access to XP.

    YS.
     
    Yohanes Santoso, Aug 31, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ABCL
    Replies:
    0
    Views:
    567
  2. Lloyd Zusman
    Replies:
    2
    Views:
    415
  3. Luke Kanies
    Replies:
    0
    Views:
    114
    Luke Kanies
    Jul 22, 2005
  4. Replies:
    7
    Views:
    158
    Bob Hutchison
    Jan 11, 2006
  5. Aaron Smith

    WEBrick::Daemon and WEBrick

    Aaron Smith, Mar 9, 2007, in forum: Ruby
    Replies:
    2
    Views:
    324
    Aaron Smith
    Mar 18, 2007
Loading...

Share This Page