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

Alan Gauld alan.gauld at btinternet.com
Wed Mar 18 23:59:35 CET 2015


On 18/03/15 18:28, Doug Basberg wrote:
> I do real-time controls programming and I control a relay in a python
> program that must be turned off before the program ends.

You don't say what your environment is but you should be aware
that Python is not really a suitable tool for real time
programming, especially if it safety critical. The same applies
to most(all?) scripting languages and especially if running
on a time sharing OS like Unix or windows.

If your code is not truly real-time or the device is not
safety critical you may get away with it. But if you are
likely to get sued for loss of life or major damage consider
your options.

That having been said there are several things you can do
to safeguard yourself. Others have mentioned the exit hooks
in the sys module, You can also try wrapping your entire main
program in a try/finally. You could also monitor the interpreter
in an external program that turns off your device directly
if the interpreter fails. But none of these can be guaranteed
to happen in real time indeed, the last option can be
guaranteed not to be...

-- 
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




More information about the Tutor mailing list