File Unlocking in __del__ does not work

schwerdy schwerdy at web.de
Wed Nov 10 06:44:22 EST 2004


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



More information about the Python-list mailing list