del behavior 2

"Martin v. Löwis" martin at
Wed Jan 7 20:59:20 CET 2009

> I understand that and have seen it too.  That's fine.  But how do any
> of you deal with things that are left open because you did not get a
> chance to close them?  How do you clean up after the fact?  Do you
> simply keep track externally the things that need to be cleaned up if
> __del__ doesn't get a chance?  Any ideas?  Thanks

You should try to write you program so that any kind of process exit
will not need any cleanup. For many kinds of things, this will work
automatically on most file systems. For example, file handles and
network connections get automatically closed - so you don't absolutely
have to close them if your program exits abnormally. Likewise, database
connections will shut down properly, and windows will close just fine.

What kind of thing do you have that remains open even after the
process terminates?


More information about the Python-list mailing list