Linux file locks ?

Robert k.robert at gmx.de
Sun Mar 23 16:04:59 EST 2003


Need a simple but strict and secure exclusive file (global) lock. "if not
os.path.exist: open(..'w') ... "  is weak - you get no guarantee.

I ended in:

-----
# access the unique lock ...
lock=open(fn_lock,'w')
import fcntl
try: fcntl.lockf(lock, fcntl.LOCK_EX|fcntl.LOCK_NB)    #exclusive lock
except IOError:
    fcntl.lockf(lock, fcntl.LOCK_UN)    #if I don't do this the second trial
overruns an other process !?
    raise
try:
    ...protected mainloop...
finally:
    lock.close();
----

but whats funny, even if a file is opened and locked this way, it maybe
deleted (rm
x.lock) without a notice!? How would one lock a file against removing ?

Is there a simple method to get a nice stable lock?

Robert









More information about the Python-list mailing list