I have a file results.txt
on a server which is accessed by multiple VMs through NFS. A process runs on each of these VMs which reads the results.txt
file and modifies it. If two processes, A
and B
, read the file at the same time, then modification of either A or B would be present in results.txt
based on the order in which the processes write to the file.
If process A
has a write lock over the file then process B
would have to wait till the lock is released to read the results.txt
file.
I have tried implementing this using Python:
import fcntl
f = open("/path/result.txt")
fcntl.flock(f,fcntl.LOCK_EX)
#code
It works as expected for files on the local disk.
but when I run try to lock a file on the mounted path, I get the following error:
Traceback (most recent call last):
File "lock.py", line 12, in <module>
fcntl.flock(f,fcntl.LOCK_EX)
IOError: [Errno 45] Operation not supported
I tried fcntl.fcntl
and fcntl.flock
but got the same error. Is this an issue with the way I am using fcntl
? Is any configuration required on the server where file is stored?
Edit:
This is how I am using fcntl.fcntl
:
f= open("results.txt")
lockdata = struct.pack('hhllhh', fcntl.F_RDLCK,0,0,0,0,0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)
The NFS server version is 3.
fcntl.fcntl()
? – Taddeofcntl.fcntl(f, fcntl.LOCK_EX)
work? – Taddeo