Skip to content
Advertisement

How to create a system-wide file lock?

I am trying to have a number of independent processes coordinate their writing to file using a system-wide lock (see here: Concurrent file accesses from different scripts python)

The lock needs to span the whole system, because the processes are spawn independently and at different times. Here: System-wide mutex in Python on Linux I read that fcntl.lockf should do what I want, but I can’t get it to work. Here is what I tried:

Terminal 1

>>> import fcntl
>>> f = open('myfile', 'w')
>>> fcntl.lockf(f, fcntl.LOCK_EX)
[Detach from terminal]

Terminal 2

>>> f = open('myfile', 'w')
>>> f.write('hello')
5
>>> f.close()
[Detach from terminal]

If I check the file, it contains ‘hello’. So there was no lock! What did I do wrong? I have tried with both ubuntu16.04 and macOS High sierra, and got the same result. I am using python 3.6

Advertisement

Answer

There is support in *NIX to apply locks to files (or parts of files). It requires the processes to co-operate with each other (advisory locking). Each process should always check if a given file or record is locked before accessing it. If this check is not performed there is no built in protection to avoid possible corruption. So in your case both processes have to use file locking for this to work.

It is good idea, if only documentary, to use LOCK_UN (unlock) explicitly, although locks are released automatically when a process exits or when the file is closed.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement