--- portalocker.py 2004/11/10 15:05:46 1.1 +++ portalocker.py 2004/11/10 15:13:15 @@ -2,7 +2,7 @@ # Requires python 1.5.2 or better. # ID line added by richard for Roundup file tracking -# $Id: portalocker.py,v 1.1 2004/11/10 15:05:46 moraes Exp $ +# $Id$ """Cross-platform (posix/nt) API for flock-style file locking. @@ -120,23 +120,25 @@ win32file.UnlockFile(hfile, 0, 0, FFFF0000, 0) elif os.name =='posix': + lockmethod = fcntl.lockf # works over NFS on most systems def lock(file, flags): - fcntl.flock(file.fileno(), flags) + lockmethod(file.fileno(), flags) # TODO: should this return the result of the lock? def unlock(file): - fcntl.flock(file.fileno(), fcntl.LOCK_UN) + lockmethod(file.fileno(), fcntl.LOCK_UN) if __name__ == '__main__': from time import time, strftime, localtime - import sys + import sys, socket import portalocker log = open('log.txt', "a+") portalocker.lock(log, portalocker.LOCK_EX) - timestamp = strftime("%m/%d/%Y %H:%M:%S\n", localtime(time())) - log.write( timestamp ) + timestamp = strftime("%m/%d/%Y %H:%M:%S", localtime(time())) + log.write( "%s %d@%s\n" % (timestamp, os.getpid(), socket.getfqdn())) + log.flush() print "Wrote lines. Hit enter to release lock." dummy = sys.stdin.readline()