Order of object cleanup at interpreter exit?

Jason Stokes jstok at bluedog.apana.org.au
Mon Feb 14 08:33:04 EST 2000


Rob W. W. Hooft wrote in message ...
>I am writing a package (98% python) that controls an analytical robot.
>The robot is controlled via a simple text protocol over a socket
>connection.
>
>When the robot control object is garbage-collected, the __del__ method
>takes care of a proper termination of the socket connection by sending
>a last "EXIT" command:


[snip]

>And indeed, it seems that the "sendfixed" function was already garbage
>collected: "print sendfixed" just before the call reports "None"....
>
>I was not aware that it was "illegal" to call a function from a destructor.
>Is this a strange caveat? Bug? Feature? Any way to avoid it?


The order names are cleaned up at the time of system exit is undefined.
This means you cannot rely on the names in your __del__ method being around
when you call them.  In other words, don't rely on implicit cleanup code
when the system exits, because it's unreliable.  Instead, explicitly clean
up by, say, creating an explicit shutdown step.

This is a gotcha, rather than a bug or a feature.







More information about the Python-list mailing list