473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Portable, generic, graceful termination

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.
Jul 18 '05 #1
0 1344

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim McGrail | last post by:
Background: I am investigating a problem involving a windows .NET application that is being developed in C# with Visual Studio 2003. It is a multi-threaded application that uses MSMQ to...
5
by: Arnaud Legrand | last post by:
Hello, I have a question about portability and I have not found the answer in the FAQ. I have different modules to build. All of them have the same private part and a common public part (though...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
9
by: ehabaziz2001 | last post by:
I am facing that error message with no idea WHY the reason ? "Abnormal program termination" E:\programs\c_lang\iti01\tc201\ch06\ownarr01o01 Enter a number : 25 More numbers (y/n)? y...
2
by: csgonan | last post by:
I have a new 64 bit apache 2.2.4 server on Solaris 10 with openssl 0.9.8e. When I DO NOT have the ssl.conf file included and I "apachectl graceful" to apache, all my processes that are gracefully...
0
by: vsankar9 | last post by:
It's an Service Contracts From CRM. I need termination amount for who's the customer is terminated . End date - 15-DEC-2007 Termination as of date - 15-MAR-2007 Rate - 1000 (monthly rate ) ...
18
by: Toms hilidhe | last post by:
(SHA-1 is a cryptographic hash function. For info on what SHA-1 is: http://en.wikipedia.org/wiki/SHA-1) I'm writing fullportable C89 code that needs to make use of the SHA-1 algorithm. Does...
69
by: Bill Reid | last post by:
This is how I handle a check that the last character of a text file is a newline: /* checks if newline is last character of text file */ unsigned check_text_file_newline_termination(FILE...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.