File locking using fcntl.flock()

Richard Jones rjones at ekit-inc.com
Tue Apr 16 21:05:53 EDT 2002


On Wed, 17 Apr 2002 03:38, Donn Cave wrote:
> Quoth Richard Jones <rjones at ekit-inc.com>:
> | I can't get any locking to work on Solaris 5.7 using python 2.1.3. On
> | Linux 2.4.9, I get an exception when I try to re-lock something that's
> | locked. On Solaris, I don't get the exception (and without LOCK_NB, I
> | don't block either). I've also tried the lockf and posixfile.lock methods
> | to no avail.
> |
> |>>> import fcntl
> |>>> f=open('foo', 'w')
> |>>> fcntl.flock(f.fileno(), fcntl.LOCK_EX)
> |>>> f2=open('foo', 'w')
> |>>> fcntl.flock(f2.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
>
> I don't have Solaris, but I bet it doesn't have flock.

It does, under the heading of "BSD compatibility".

That's almost a moot point though, because I also mentioned that I tried both 
fcntl.lockf() ("This is essentially a wrapper around the fcntl() locking 
calls.") and posixfile.lock() and neither of those worked either. The _real_ 
problem here is that Solaris appears to allow a process to have two locks on 
a single file in the same process. Things are fine if the lock is requested 
from separate processes.

Thus in separate processes fcntl.lockf() and friends work fine. This is 
troublesome because threads can't use file locks. Just gotta avoid threading 
:(


    Richard






More information about the Python-list mailing list