Exiting gracefully from ThreadingTCPServer

Discussion in 'Python' started by Pete Emerson, Mar 12, 2010.

  1. Pete Emerson

    Pete Emerson Guest

    I'm trying to get threading going for the first time in python, and
    I'm trying to modify code I found so that I can have the server close
    the TCP connections and exit gracefully. Two problems:

    1) While the KeyboardInterrupt works, if I make more than 0 curls to
    the server and then quit, I can't run it again right away and get
    this:
    socket.error: [Errno 48] Address already in use

    Not all of my connections are closing properly. How do I fix this?

    2) curling localhost:8080/quit does show the "Quitting" output that I
    expect, but doesn't quit the server until I manually control-c it.

    I think that I need *all* threads to close and not just the current
    one, so I'm not quite sure how to proceed. Pointers in the right
    direction are appreciated. And if there's a "better" way to do this
    threading httpd server (subjective, I realize), please let me know!
    Thanks.

    Pete

    #############################################
    #!/usr/bin/env python

    import SocketServer
    import SimpleHTTPServer

    PORT = 8080
    done = False
    class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
    global done
    if self.path=='/quit':
    self.send_response(200)
    self.send_header('Content-type','text/html')
    self.end_headers()
    self.wfile.write('Quitting')
    done = True
    return self
    else:
    self.send_response(200)
    self.send_header('Content-type','text/html')
    self.end_headers()
    self.wfile.write('Unknown')
    return self


    if __name__ == "__main__":
    httpd = SocketServer.ThreadingTCPServer(('localhost',
    PORT),CustomHandler)
    try:
    while not done:
    print "done: ", done
    httpd.handle_request()
    except KeyboardInterrupt:
    print "Server is done."
    httpd.server_close()
     
    Pete Emerson, Mar 12, 2010
    #1
    1. Advertising

  2. En Fri, 12 Mar 2010 12:15:16 -0300, Pete Emerson <>
    escribió:

    > I'm trying to get threading going for the first time in python, and
    > I'm trying to modify code I found so that I can have the server close
    > the TCP connections and exit gracefully. Two problems:


    Which Python version?

    > 1) While the KeyboardInterrupt works, if I make more than 0 curls to
    > the server and then quit, I can't run it again right away and get
    > this:
    > socket.error: [Errno 48] Address already in use
    >
    > Not all of my connections are closing properly. How do I fix this?
    >
    > 2) curling localhost:8080/quit does show the "Quitting" output that I
    > expect, but doesn't quit the server until I manually control-c it.


    On Python 2.6, you should call the shutdown() method (from another
    thread!) instead of simply server_close()
    See this recipe [1] -- just look at the main() function (disregard all the
    previous details); it runs the server(s) in their own thread, the main
    thread just sits and waits for KeyboardInterrupt or SystemExit.

    [1] http://code.activestate.com/recipes/577025

    --
    Gabriel Genellina
     
    Gabriel Genellina, Mar 17, 2010
    #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. UrgeOverkill
    Replies:
    2
    Views:
    481
    Peter Hansen
    Feb 19, 2004
  2. Dustin
    Replies:
    2
    Views:
    1,082
    G. S. Hayes
    Aug 9, 2004
  3. Stuart D. Gathman

    Debugging SocketServer.ThreadingTCPServer

    Stuart D. Gathman, Jan 16, 2007, in forum: Python
    Replies:
    0
    Views:
    342
    Stuart D. Gathman
    Jan 16, 2007
  4. Jean-Paul Calderone

    Re: Debugging SocketServer.ThreadingTCPServer

    Jean-Paul Calderone, Jan 16, 2007, in forum: Python
    Replies:
    2
    Views:
    404
    R. Bernstein
    Feb 4, 2007
  5. vkaushal21

    Exiting socket gracefully

    vkaushal21, Sep 7, 2009, in forum: C Programming
    Replies:
    0
    Views:
    386
    vkaushal21
    Sep 7, 2009
Loading...

Share This Page