F_GETLK Working?

Raja raja0576 at yahoo.com
Wed Mar 17 00:28:40 EST 2004


**Newbie Alert**

I was trying to check for a lock on a file that i had acquired
earlier. If i try and reacquire the lock, it throws a exception saying
resource temporarily unavailable. But i though F_GETLK should be the
better way to check for existance of a lock.
This is how my code looks
lock_file = file('lockedfile', 'w')
fcntl.fcntl(lock_file, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
flk=struct.pack('hhllhl',fcntl.F_WRLCK,0,0,0,0,0)
try:
    rv = fcntl.fcntl(lock_file, fcntl.F_GETLK, flk)
except IOError:
    print "Error getting lockstatus"
    raise
locked,a,b,c,d,e= struct.unpack('hhllhl', flk);
print locked

I was expecting the first argument of the flk(unpacked) to contain
F_UNLCK (as per
http://www.ecst.csuchico.edu/~beej/guide/ipc/flock.html - Section on
F_GETLK) but i get the same value that i pass in i.e. if i pass in
F_WRLCK i get back a F_WRLCK. Am i doing something wrong?

What is the best way to check for the existance of a lock?

Thanks a lot
Raja



More information about the Python-list mailing list