[Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program?

Steven D'Aprano steve at pearwood.info
Sat Mar 21 02:03:17 CET 2015


On Fri, Mar 20, 2015 at 08:35:34PM +0000, Alan Gauld wrote:

> Yeah, I know you can catch a signal and add your own handler, but I 
> meant what is the default Python suspend behaviour? Does it execute any 
> outstanding exception blocks? What about finally blocks? Or, if about to 
> exit a context manager, the __exit__ method?

Depends on the signal.

I'm not an expert on how signals work in Linux, or other Unixes, but I 
expect that, in the absense of a specific signal handler to catch it, 
the "sleep" (pause?) signal will cause the interpreter to just stop and 
wait. I think that's signal 19 on Linux, and 18 to wake.

Signal 9 doesn't give the interpreter to do anything. The OS just yanks 
the carpet out from under its feet and terminates the process with 
extreme prejudice. Signal 9 cannot be caught, no signal handlers will 
detect it, no try...finally blocks will run. The process just stops.

Don't use kill -9 unless you need to. I always try three steps to kill a 
rogue process:

First use "kill <processid>" with no other arguments. Give it 30 seconds 
or so to let the process tidy up after itself, and if it still hasn't 
quiet, try "kill -HUP <processid>". Again, give it 30 seconds or so. 
Then, if and only if necessary, "kill -9 <processid>".


> Or does it just stop and wait till its resumed? Kind of like
> an implicit yield statement?





> 
> 
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list