no cleanup on TERM signal

Laszlo Nagy gandalf at shopzeus.com
Fri May 2 11:17:10 EDT 2008


> 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




More information about the Python-list mailing list