[Tutor] Final de-allocation before the application exists

Jeff Shannon jeff@ccvcorp.com
Tue Jul 22 12:46:02 2003


K Dhanvi wrote:

>  I would like to know whether Python has a function which would be 
> called just before the end of the application. I cannot use 
> destructors in my application because I have some static data in my 
> application that is modified during the life cycle of the 
> application.  If I have any function like this , then I could hide the 
> gory details of de-allocation of resources completely from the user. 
> This way, what he ( user ) does not know, he need not be bothered 
> about :)..


This depends a bit on just what you're thinking of de-allocating.  In 
most cases, when Python closes, it will automatically clean up after 
itself.  This includes freeing memory, closing open files, releasing 
sockets, etc, etc.  So, you *probably* don't actually need to do this 
yourself.  The big exception would be if that static data you mention 
needs to be written to disk before the application exits.  (Threads 
cannot be closed from outside that thread, they must stop themselves, 
but they do so automatically when the application closes unless the 
thread has been marked as a daemon, in which case they prevent the app 
from closing.)  The best way to do that depends very much on the 
structure of your program.  Most (all?) GUI toolkits allow you to hook 
into window-closing and application-closing events; a text menu-driven 
app would probably have an exit option that could be expanded on.  Tell 
us a bit more about what you need to do at closing, and how your program 
operates, and we can probably give a little bit better advice...

Jeff Shannon
Technician/Programmer
Credit International