fixing the ForkingTCPServer?
Matt Dunford
kno at jtan.com
Wed Dec 8 14:20:46 EST 1999
The default ForkingTCPServer didn't seem to be cleaning up its children properly. The only way I could get it to clean up
after itself and therefore get rid of zombie processes was by overridding the serve_forever and collect_children methods in
the ForkingTCPServer class. If what I did safe?
#!/usr/local/bin/python
HOST = ''
PORT = 4115
from SocketServer import *
from string import strip
import posix
class MyServer(ForkingTCPServer):
# override serve_forever def
def serve_forever( self ):
while 1:
self.handle_request()
self.collect_children()
# override collect_children
# because the default doesn't work
def collect_children(self):
"""Internal routine to wait for died children."""
while self.active_children:
pid, status = posix.waitpid(0, posix.WNOHANG)
if not pid: continue
self.active_children.remove(pid)
class echohandler(StreamRequestHandler):
# put whatever you want the session to
# do in the handle def, in this case
# it echos input back to output
def handle(self):
while 1:
str = self.rfile.readline( 16384 )
if strip(str) == 'quit': break
self.wfile.write(str)
if __name__ == '__main__':
from socket import *
server = MyServer((HOST, PORT), echohandler )
server.serve_forever()
--
More information about the Python-list
mailing list