473,657 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s ignal.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(s ignal.SIGTERM, handler)

def hook(type, value, traceback, previous = sys.excepthook) :
if not isinstance(valu e, 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("S omething 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 1352

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

Similar topics

2
2084
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 communicate between the threads. The problem is that the program infrequently terminates with no indication of why it terminated.
5
1656
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 the implementation in all cases may be different). Note that I already have implemented a similar idea and that it works pretty well with my gcc but I don't know whether it is portable or not (especially with those damn padding bytes... ;). As a...
131
6108
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 portable C code - *not only* because, in a 'C sense', I
669
25858
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
9
6885
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 Enter a number : 30 More numbers (y/n)? n
2
4262
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 shutdown are shown by a dash (-). When I have the Include line for the ssl.conf file uncommented, the number of Gs never changes, like it is locked in the graceful shutdown. I have prefork compiled in but not worker or event if that makes a...
0
1419
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 ) Termination value = value of 16 days of march + value for 8 months ( april to nov) + value of 15 days of Dec
18
2425
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 anyone know if there's been a fully-portable C89 implementation of the SHA-1 algorithm? If not, could someone please point me to a very good implementation of the algorithm, an implementation which is very portable (perhaps portable to machines...
69
2614
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 *test_file) { int end_char; fseek(test_file,-1L,SEEK_END); end_char=getc(test_file);
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7320
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.