我将python2.6与HTTPServer
和{
下面是请求处理程序的大致情况
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()
问题是如果我停止服务器,仍有一些线程在等待rfile.readline()
。在
我会在readline()
上面放一个select([rfile, closefile], [], [])
并在想关闭服务器时写入closefile,但我认为它在windows上不起作用,因为select
只适用于套接字。在
我的另一个想法是跟踪所有正在运行的请求和rfile.close()
,但是我遇到了断管错误。在
有什么想法?在
就快到了,正确的方法是调用
rfile.close()
,捕捉断开的管道错误,并在发生这种情况时退出循环。在如果在
HTTPServer
子类中将daemon_threads
设置为true,则线程的活动不会阻止服务器退出。在您也可以通过使
closefile
成为一个套接字来解决Windows问题。毕竟,由于它可能是由主线程打开的,所以您可以决定是将其作为套接字还是文件打开;-)。在相关问题 更多 >
编程相关推荐