[Web-SIG] Daemon server management
davidf at sjsoft.com
Tue Jun 14 13:46:20 CEST 2005
David Fraser wrote:
>Ian Bicking wrote:
>>As a followup on this, this is what I have so far:
>> f = open(pidfile)
>> pid = int(f.read().strip())
>> 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))
>> print "Can't start, another server running (%i)" % pid
>>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:
>>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)?
I found glock from http://rgruet.free.fr/
It's quite nice except that it had a few problems when I tried it, so I
Basically you can create a GlobalLock that uses flocks on Unix and named
mutexes on Win32, and acquire / release them.
Tested and have a nice handover system working in jToolkit which works
by having a socket lock that controls access to the socket, and a series
of running locks which control the handover process
Basic logic as follows:
1) Starts up, creates socket lock called "socket-$pid" and running
lock called "running-$pid-$n" (starts at n=1)
2) Starts child process, passing socket lock name and running lock
name on command line
3) Waits for child process to acquire running lock
4) Waits for child process to release running lock
5) n += 1, creates new running lock "running-$pid-$n"
5) go to 2
1) Acquires Running lock
2) Acquires Socket lock
3) Binds socket, starts listening for requests
4) When listened to maxrequests, releases running lock
5) waits for some process to acquire "running-$pid-n+1" (handling
requests in the mean time)
6) closes the socket, and releases the socket lock
My altered glock.py is at http://davidf.sjsoft.com/files/glock.py
More information about the Web-SIG