Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to shutdown a threaded HTTP server with persistent connections (how to kill readline() from another thread)?

I'm using python2.6 with HTTPServer and the ThreadingMixIn, which will handle each request in a separate thread. I'm also using HTTP1.1 persistent connections ('Connection: keep-alive'), so neither the server or client will close a connection after a request.

Here's roughly what the request handler looks like

request, client_address = sock.accept()
rfile = request.makefile('rb', rbufsize)
wfile = request.makefile('wb', wbufsize)

global server_stopping
while not server_stopping:
    request_line = rfile.readline() # 'GET / HTTP/1.1'
    # etc - parse the full request, write to wfile with server response, etc
wfile.close()
rfile.close()
request.close()

The problem is that if I stop the server, there will still be a few threads waiting on rfile.readline().

I would put a select([rfile, closefile], [], []) above the readline() and write to closefile when I want to shutdown the server, but I don't think it would work on windows because select only works with sockets.

My other idea is to keep track of all the running requests and rfile.close() but I get Broken pipe errors.

Ideas?

like image 614
Alex Avatar asked Jun 02 '26 02:06

Alex


1 Answers

You're almost there—the correct approach is to call rfile.close() and to catch the broken pipe errors and exit your loop when that happens.

like image 162
Greg Hewgill Avatar answered Jun 03 '26 16:06

Greg Hewgill