Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Portable, generic, graceful termination

Reply
Thread Tools

Portable, generic, graceful termination

 
 
Alexander Staubo
Guest
Posts: n/a
 
      09-28-2004
Python does not seem to clean up gracefully on SIGTERM: The exit
sequence is not invoked on termination, so the atexit module cannot be
used to install shutdown logic.

Further, while the signal module allows trapping the signal, it does
not offer a truly portable way of respecting the termination request.
As far as I can see, this issue has been a topic of conversation for
years, but I don't see that it has ever been solved:

http://www.python.org/search/hyperma...1992/0206.html

This is what I came up with initially:

previousHandler = None
def handler(signum, frame):
if previousHandler:
previousHandler(signum, frame)
else:
os._exit(128 + signal.SIGTERM)
previousHandler = signal.signal(signal.SIGTERM, handler)

This at least 1) preserves the Python exit sequence, 2) follows Unix
conventions, and 3) is friendly to libraries or anyone else who have
installed their own signal handlers.

I am, however, concerned with the fact that it could be, as Guido
points out, distruptive to things going on concurrently in the
interpreter.

The other, seemingly more elegant solution is to invent your own
exception and add a global exception hook:

class Dummy(Exception): pass

previousHandler = None
def handler(signum, frame):
if previousHandler:
previousHandler(signum, frame)
else:
raise Dummy()
previousHandler = signal.signal(signal.SIGTERM, handler)

def hook(type, value, traceback, previous = sys.excepthook):
if not isinstance(value, Dummy):
previous(type, value, traceback)

This technique has the downside of possibly being caught by alien
code. For example, a signal occuring during the following code
snippet's execution will effectively be ignored:

try:
# ... something ...
except:
logger.error("Something went wrong", exc_info = True)

Would someone please come along with an incredibly elegant hack that
everyone but I knows about?

Alexander.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a small graceful gcd function? lovecreatesbeauty C Programming 73 07-26-2006 02:02 AM
Graceful failover uofitorn@gmail.com Cisco 3 03-22-2005 04:12 PM
Graceful detection of EOF MickeyBob Python 20 10-14-2004 09:29 AM
graceful file upload limit error? Tom Kaminski [MVP] ASP .Net 6 07-20-2004 06:53 PM
Graceful failures Jacob H Python 3 12-31-2003 08:28 PM



Advertisments