daemonizing after binding to port

mk mrkafk at gmail.com
Fri Jun 18 13:47:57 EDT 2010


Hello everyone,

I'm starting a SocketServer.TCPServer in my program, but since I want to 
report problems to script starting the program, I want to go daemon 
*after* TCPServer has done binding to port.

Is this likely to cause problems? I mean, my client works when I do the 
above, that is, it connects to the server. But I'm not sure if above is 
the Right Thing(tm).

First:

self.server = SocketServer.TCPServer((host, port), handleclass, 
bind_and_activate=False)
self.server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.server_bind()
self.server.server_activate()

(except etc skipped for brevity)

Second:

def daemonize(self):
     try:
         pid = os.fork()
     except OSError, e:
         print 'failed to fork:', str(e)
         os._exit(1)
     # if pid is non-zero, we're in parent
     if pid:
         os._exit(0)
     os.setsid()
     os.chdir(globalpath)
     os.umask(0)




Regards,
mk




More information about the Python-list mailing list