[Baypiggies] What is destructor expected behavior?

Alex Martelli aleax at google.com
Wed Feb 27 22:32:56 CET 2008

On Wed, Feb 27, 2008 at 12:08 PM, Glen Jarvis <glen at glenjarvis.com> wrote:
> Summary: Can I assume, 100% of the time, that a destructor is called before
> a program ends?

No (reference loops for example inhibit that).

> If not, what is the design pattern to "time" issues in Python automatically
> at program begin time/end time? Futher granularity into the object level
> would be very helpful and preferred.

See http://docs.python.org/lib/module-atexit.html for a way to to
ALMOST this; however, for a sufficiently hard crash of a program, no
such guarantee can be offered in-process (in Python's atexit case, the
lack of guarantee also applies to program-killed-by-signal and
os._exit calls, but even if you could bypass those sufficiently hard
crashes would STILL invalidate the guarantee, e.g. kill -9 ENSURES no
more code in the harshy-killed process will run).  The architecture
pattern that may help with that issue is to have a separate "watchdog
process" that detects whether the target process is alive or dead
(exactly how best to do that varies by operating system, since the
detection depends on services the OS is providing to your processes):
that might trigger some code to run no matter how harshly and abruptly
the target process dies (however you will need to run the watchdog on
separate hardware if you also want to catch cases in which the whole
machine running the target process suddenly loses power, so its OS
also dies with no chance of recovery -- etc, etc).

So, in brief, it depends on the level of guarantee you need...


More information about the Baypiggies mailing list