Exiting gracefully from ThreadingTCPServer

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Mar 16 22:21:14 EDT 2010


En Fri, 12 Mar 2010 12:15:16 -0300, Pete Emerson <pemerson at gmail.com>  
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




More information about the Python-list mailing list