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