linuxnewbie: clean handling of SIGTERM
Randall Kern
randy at spoke.net
Sat Feb 3 16:02:56 EST 2001
This is hopefully a simple "everybody has done that" kind of question, but
my Linux (unix) experience is sort of lacking.
I have a daemon that runs a loop, waiting for work. When a request arrives,
it processes the request, and goes back into the loop.
Now I want to check for shutdown on each iteration of the loop. I thought I
could register a handler for SIGTERM, and simply set a variable to tell my
loop to terminate:
_shutdown = 0
def sigterm(sig, frame):
global _shutdown
_shutdown = 1
signal.signal(signal.SIGTERM, sigterm)
while not _shutdown:
request = get_request() # returns None if _shutdown gets set (select
based loop)
if request is not None:
process_request(request)
The problem is that I get various exceptions when the signal is raised
(EINTR, but in various guises depending on the code that's running). It
seems to me I could fix this if I could use reliable (BSD) signals from
python, but I don't see a way to do that.
Otherwise, I'll have to trap each and every EINTR derived exception, which
will mean copying a lot of library code (generally, a big hairy mess).
Is there a better way?
Thanks,
-Randy
P.S. My system does recover from a forced shutdown, but I would like to
complete any running requests before shutting down.
More information about the Python-list
mailing list