[Numpy-discussion] 30% speedup when deactivating NumArray.__del__ !!!

Andrew McNamara andrewm at object-craft.com.au
Sun Jan 30 16:57:34 CST 2005


>This benchmark made me suspicious since I had already found it odd before that 
>killing a numarray calculation with Ctrl-C nearly always gives a backtrace 
>starting in __del__

Much of the python machinery may have been torn down when your __del__
method is called while the interpreter is exiting (I'm asuming you're
talking about a script, rather than interactive mode). Code should
be prepared for anything to fail - it's quite common for parts of
__builtins__ to have been disassembled, etc.

The language reference has this to say:

    http://python.org/doc/2.3.4/ref/customization.html#l2h-174

    Warning: Due to the precarious circumstances under which __del__()
    methods are invoked, exceptions that occur during their execution
    are ignored, and a warning is printed to sys.stderr instead. Also,
    when __del__() is invoked in response to a module being deleted (e.g.,
    when execution of the program is done), other globals referenced by
    the __del__() method may already have been deleted. For this reason,
    __del__() methods should do the absolute minimum needed to maintain
    external invariants. Starting with version 1.5, Python guarantees that
    globals whose name begins with a single underscore are deleted from
    their module before other globals are deleted; if no other references
    to such globals exist, this may help in assuring that imported modules
    are still available at the time when the __del__() method is called.

Another important caveat of classes with __del__ methods is mentioned in
the library reference for the "gc" module:

    http://python.org/doc/2.3.4/lib/module-gc.html

    Objects that have __del__() methods and are part of a reference
    cycle cause the entire reference cycle to be uncollectable,
    including objects not necessarily in the cycle but reachable only
    from it. Python doesn't collect such cycles automatically because,
    in general, it isn't possible for Python to guess a safe order in
    which to run the __del__() methods.

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/




More information about the Numpy-discussion mailing list