473,782 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

no cleanup on TERM signal

I did a few tests with this script:

class byebye:

def __del__(self):
print 'Bye, bye...'
x = byebye()
x.del() gets executed if:
-I del x, then run gc.collect()
-simply exit the script
-get the script to abort on an exception

But if I kill it with the default signal TERM, the script dies, but I don't
get the message, so I am assuming that python isn't taking the time to
cleanup, even though that is (was) what TERM was intended for.

Has this been discussed before ? Is worth a suggestion (PEP) ?
--
Yves.
http://www.SollerS.ca
Jun 27 '08 #1
5 2522
On Fri, 02 May 2008 04:36:06 +0000, Yves Dorfsman wrote:
x.del() gets executed if:
-I del x, then run gc.collect()
-simply exit the script
-get the script to abort on an exception

But if I kill it with the default signal TERM, the script dies, but I don't
get the message, so I am assuming that python isn't taking the time to
cleanup, even though that is (was) what TERM was intended for.

Has this been discussed before ? Is worth a suggestion (PEP) ?
There is the docs for `__del__()` saying this method is not guaranteed to
be called at all. Don't use it if you *need* that method to be called.
Just like `finalize()` in Java, it can't be used for deterministic
destruction, so it's not that useful after all.

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #2
Yves Dorfsman wrote:
I did a few tests with this script:

class byebye:

def __del__(self):
print 'Bye, bye...'
x = byebye()
x.del() gets executed if:
-I del x, then run gc.collect()
-simply exit the script
-get the script to abort on an exception

But if I kill it with the default signal TERM, the script dies, but I
don't get the message, so I am assuming that python isn't taking the
time to cleanup, even though that is (was) what TERM was intended for.
TERM signal is unix specific. There is no special syntax/programming
structure for signals inside the Python language, since it would be
platform dependent. For simple client programs, usually it is not needed
to setup signal handlers because they can easily be controlled in other
ways.

For sensitive resources, instead of writing __del__ methods, you should
create a "close()" method. Python does this with file objects, DB API
2.0 with database connection objects etc. Then you can do

res = create_resource ()
try:
use_resource()
finally:
res.close() # Must free resource, but the object can still be alive...

It is more common to use signals when you have more threads or child
processes. You can use something like:

import threading
import signal

stop_requested = threading.Event ()
exited_on_sigte rm = False

def free_all_resour ces():
pass # Free your resources here!

def sigterm_handler (signum, frame):
"""Stop the server gracefully when on SIGTERM."""
global stop_requested
global exited_on_sigte rm
exited_on_sigte rm = True
stop_requested. set()
free_all_resour ces()

def main():
global stop_requested
global exited_on_sigte rm

logger = servicelog.getL ogger('main',fi lename=LOGFILEN AME)

logger.info('Se tting up the SIGTERM signal handler.')
signal.signal(s ignal.SIGTERM, sigterm_handler ) # Setup signal handler

logger.info('St arting watchdog thread')
watchdog = WatchDog()
watchdog.start( )

worker1 = create_worker(s top_requested)
worker2 = create_worker(s top_requested)
# etc. Prepare things here...

try:
try:
server = create_my_serve r()
server.serve_un til_not_stopped ()
except Exception, e:
logger.error(du mpexc(e))
raise e
finally:
stop_requested. set() # Ask other threads to stop cooperatively

# Join all threads here
watchdog.join()
worker1.join()
worder2.join()
# etc. wait for all threads to exit cooperatively.
if exited_on_sigte rm:
logger.warning( 'Exited on SIGTERM!')

Best,

Laszlo

Jun 27 '08 #3
Laszlo Nagy schrieb:
For sensitive resources, instead of writing __del__ methods, you should
create a "close()" method. Python does this with file objects, DB API
2.0 with database connection objects etc. Then you can do

res = create_resource ()
try:
use_resource()
finally:
res.close() # Must free resource, but the object can still be alive...
You can replace the try/finally code with a "with resource:
do_something()" block if the object supporst the context manager protocol.

If you want to run some code during the shutdown phase of the Python
process you can register a callback function in the "atexit" module.
It is more common to use signals when you have more threads or child
processes. You can use something like:
Do NOT mix threads and signals. It's usually a very bad idea and may
lead to surprising side effects. Signals are only handled by the main
thread.

Christian
Jun 27 '08 #4
Christian Heimes <li***@cheimes. dewrote:
>res = create_resource ()
try:
use_resource()
finally:
res.close() # Must free resource, but the object can still be
alive...

You can replace the try/finally code with a "with resource:
do_something()" block if the object supporst the context manager
protocol.
or replace it with:

with contextlib.clos ing(create_reso urce()) as res:
do_something()

if the object does not support the context manager protocol.
Jun 27 '08 #5
You can replace the try/finally code with a "with resource:
do_something()" block if the object supporst the context manager protocol.
I'm using 2.4 so...
If you want to run some code during the shutdown phase of the Python
process you can register a callback function in the "atexit" module.
I also wanted to tell this to the OP but he was asking about signals.
AFAIK atexit is only called on SIGTERM, not on others.
Do NOT mix threads and signals. It's usually a very bad idea and may
lead to surprising side effects. Signals are only handled by the main
thread.
Well, I have to, because:

#1. I have a server that uses threads. Using fork() instead of threading
would be a nightmare in my case.
#2. It must support signals, just because some users tend to use things
like "killall python" and try/finally is not useful in those cases. I
cannot do anything to prevent the users killing the server. They will
just do it...

I know that signals are handled in the main thread only. This is why I'm
using a threading.Event () object to allow all threads shut down
cooperatively: sigterm will set the event, and then all threads
(including the main thread) will exit gracefully (within a given
timeout). Do you know a better solution?

Thanks,

Laszlo

Jun 27 '08 #6

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

Similar topics

1
4060
by: Diez B. Roggisch | last post by:
Hi, google doesn't give me anything on this, so I have to ask: I've got a xmlrpc-server running using twisted. Using the cookbook-recipe for daemonizing, it stores a file with its pid when in daemon mode. Now I want to remove that file when the server is shutdown - this boils down to the twisted reactors run()-method beeing terminated.
0
4202
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT signals while in the readline() function. In C, when I've set a SIGINT handler using signal(), it is immediately called (even in readline()) when I hit CTRL-C. Likewise in Perl, if I am reading in a while() loop from STDIN, and I have set the...
0
3366
by: steffen staehle | last post by:
Hi, I'm writing a server process which spawns child processes via fork This server process should keep track of the number of children stil running, do the necessary cleanup to avoid zombies, etc. In the beginning I thought I had to use the POSIX module, using SigSet SigAction, sigprocmask etc. for a rather paranoid approach. Then I've rea
2
2212
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only suggestion on how to deliver a signal to an object is to do it via a global variable and a wrapper function, a technique that is generally a Bad Idea (due to the usage of a global variable). I understand that this ng is dedicated to the discussion of...
3
3525
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/ signal(SIGINT,crash); signal(SIGBUS,crash); signal(SIGSEGV,crash); This part works. Those signals cause the "crash.c" module to run and get rid of the lock. Is there a standard way to also print
11
2974
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
7
1701
by: Amit Sharma | last post by:
Hi, I want to write a program, where until we give the value of j as non zero, it should go on asking us values of i and j, displaying the message "Division by zero: Floating point exception" for every input of j as zero. Once we give the value of both i and j as non-zero, it displays i/j and terminates. This is the sample code i tried: #include <signal.h> void sig_s(int signo);
6
5196
by: g35rider | last post by:
Hi, sometimes due to unchecked code I get segmentation faults once in a while and would like to be able to catch them and do some cleanup on things. This segmentation fault could be anywhere in the code. Could it be caught like a signal and perform cleanup then? Or would I have to put every piece of function code in try and catch blocks? Thanks Ankur
1
2485
by: whatisron | last post by:
I'm trying to do some cleanup (write open files) when Linux shuts down. I thought the right method would be to trap SIGTERM and do the necessary processing. Here's my sample code: #include <stdio.h> // for File I/O #include <signal.h> // for signals #include <unistd.h> // for sleep() void handler(int signal) { FILE *out=fopen("test.txt","at");
0
9641
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
10313
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...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8968
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 project—planning, coding, testing, and deployment—without 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
6735
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
5378
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...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.