File Unlocking in __del__ does not work

Discussion in 'Python' started by schwerdy, Nov 10, 2004.

  1. schwerdy

    schwerdy Guest

    Hello developers!

    I'm using Python 2.3.4 under debian Sarge and want to write a small
    logger class. My source code reads:

    #***************************************************
    import sys, time
    from fcntl import *
    class Log(object):
    """
    Very Simple Logger Class
    """
    def __init__(self, path):
    self.logfile = open(path, 'a')
    flock(self.logfile, LOCK_EX | LOCK_NB) # throw exept. if file
    locked

    def write(self, msg):
    self.logfile.write(time.strftime('%Y-%m-%d %H:%M:%S : ') +
    str(msg) + '\n')
    self.logfile.flush()

    def __del__(self):
    flock(self.logfile, LOCK_UN)
    self.logfile.close()


    l = Log('/var/log/myagi.log')
    log = l.write

    log("bla")
    #***************************************************

    When I run the script, python says "Exception exceptions.TypeError:
    "'NoneType' object is not callable" in <bound method Log.__del__ of
    <__main__.Log object at 0x401e238c>> ignored"
    It seems, as in the __del__ method, the flock function has gone (a
    debug line as "print flock" in the __del__ method prints "None").

    does anybody know what is going on?

    best regards,
    Sebastian 'Schwerdy' Schwerdhoefer
    schwerdy, Nov 10, 2004
    #1
    1. Advertising

  2. schwerdy

    Terry Reedy Guest

    "schwerdy" <> wrote in message
    news:...
    > #***************************************************
    > import sys, time
    > from fcntl import *
    > class Log(object):
    > """
    > Very Simple Logger Class
    > """
    > def __init__(self, path):
    > self.logfile = open(path, 'a')
    > flock(self.logfile, LOCK_EX | LOCK_NB) # throw exept. if file
    > locked
    >
    > def write(self, msg):
    > self.logfile.write(time.strftime('%Y-%m-%d %H:%M:%S : ') +
    > str(msg) + '\n')
    > self.logfile.flush()
    >
    > def __del__(self):
    > flock(self.logfile, LOCK_UN)
    > self.logfile.close()
    >
    >
    > l = Log('/var/log/myagi.log')
    > log = l.write
    >
    > log("bla")
    > #***************************************************
    >
    > When I run the script, python says "Exception exceptions.TypeError:
    > "'NoneType' object is not callable" in <bound method Log.__del__ of
    > <__main__.Log object at 0x401e238c>> ignored"
    > It seems, as in the __del__ method, the flock function has gone (a
    > debug line as "print flock" in the __del__ method prints "None").
    >
    > does anybody know what is going on?


    A wild guess: your quoted script does not explicitly delete instance l and
    method log (which contains an bound reference to the same object).
    Therefore, the object will only be deleted and __del__ invoked as part of
    the shutdown cleanup process. In the absence of explicitly registered
    cleanup functions, cleanup happens in arbitrary implementation and
    version-specific order. It just happens that flock is None'ed before l.
    So what happens if you do do an explicit 'del l,log' at the end of your
    script?

    Note: on Jython, even explicit delete is not enough to trigger the __del__
    method. Better to rename __del__ as 'close' and call l.close() to release
    the resources.

    Terry J. Reedy
    Terry Reedy, Nov 10, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3VuZGFyYXJhamFu?=

    Unlocking db objects in Session end

    =?Utf-8?B?U3VuZGFyYXJhamFu?=, Jul 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    349
    Andy Fish
    Jul 22, 2005
  2. NaeiKinDus
    Replies:
    1
    Views:
    558
    Jack Klein
    Apr 14, 2007
  3. NaeiKinDus
    Replies:
    3
    Views:
    589
    James Kanze
    Apr 15, 2007
  4. Justin Dutoit

    Manual Unlocking Users?

    Justin Dutoit, Apr 4, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    374
    Cowboy \(Gregory A. Beamer\)
    Apr 4, 2008
  5. Brandon Lepley

    DB not unlocking?

    Brandon Lepley, Sep 9, 2004, in forum: ASP General
    Replies:
    2
    Views:
    129
    Brandon Lepley
    Sep 10, 2004
Loading...

Share This Page