locking files on Linux

andrea crotti andrea.crotti.0 at gmail.com
Thu Oct 18 09:14:02 EDT 2012


I'm trying to understand how I can lock a file while writing on it,
because I might have multiple processes working on it at the same time.

I found the fcntl.lockf function but if I do this:

In [109]: locked = open('locked.txt', 'w')

In [110]: fcntl.lockf(locked, fcntl.LOCK_EX)

I can happily open the file with vim from somewhere and write on it, so
it doesn't seem to be very useful, or am I missing something?

I can otherwise use the classic ".lock" file when working on it, but I
would be glad to see a smarter solution (should only work on Linux)..



More information about the Python-list mailing list