Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Exiting gracefully from ThreadingTCPServer (http://www.velocityreviews.com/forums/t717547-exiting-gracefully-from-threadingtcpserver.html)

Pete Emerson 03-12-2010 03:15 PM

Exiting gracefully from ThreadingTCPServer
 
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.SimpleHTTPRequestHa ndler):
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()

Gabriel Genellina 03-17-2010 02:21 AM

Re: Exiting gracefully from ThreadingTCPServer
 
En Fri, 12 Mar 2010 12:15:16 -0300, Pete Emerson <pemerson@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



All times are GMT. The time now is 10:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.