[Web-SIG] Daemon server management

David Fraser davidf at sjsoft.com
Mon Jun 13 12:47:16 CEST 2005

Ian Bicking wrote:

>As a followup on this, this is what I have so far:
>When starting:
>if os.path.exists(pidfile):
>     f = open(pidfile)
>     pid = int(f.read().strip())
>     f.close()
>     try:
>         os.kill(pid, signal.SIG_DFL) # SIG 0, should do nothing
>     except OSError:
>         # Means no such process exists
>         log('PID file %s exists, but process %i no longer running'
>             % (pidfile, pid))
>         os.unlink(pidfile)
>     else:
>         print "Can't start, another server running (%i)" % pid
>         sys.exit(1)
>I guess there's some possible race conditions at this point.  But I'm 
>not sure how to resolve those.  I guess I could do Jacob's 
>fd=os.open(fname, os.O_CREATE | os.O_EXCL) later.  And match instead of 
>os.path.exists() and open() I should open the pid file similarly in that 
>code block above.
>Later, when killing the process, I'm doing:
>os.kill(pid, signal.SIGTERM)
>But I'm not sure if that's right either.  The process seems to die, 
>instead of properly terminating.  Should I send another signal, and set 
>up a signal handler in the server?  Then perhaps I would send that 
>signal, wait a bit, and send SIGTERM if it didn't stop on its own?
It would be really nice if there was a builtin Python module that 
handled interprocess mutexes, locks etc on a high level, in the same way 
that threading does (but for processes, not threads). I've found most of 
my time dealing with this issue taken up with dealing with the 
interprocess locking on a low level.

Anyone know of any such modules (not neccessarily builtin, of course)?


More information about the Web-SIG mailing list