How to lock a file in linux (or cross platform?)

Horst Eyermann horst at eaglesnest.mceggman
Fri Jan 4 05:02:08 EST 2002


Hello,
I do have problems obtaining a file lock under linux.
(linux 2.4.16 / python 2.1)

I do:

import time
import struct, fcntl, FCNTL


dict = "test.txt"

file = open(dict, "rwb")
rv = fcntl.fcntl(file.fileno(), FCNTL.F_SETFL, FCNTL.F_WRLCK)
print rv
print "lock"
time.sleep(30)

when I do it, the returned rv is "0", which is what I expect,
but when I start the program twice (a second apart),
I recieve "0" twice, where I would expect to get a failure
from the second command.

I am not familiar with file locking, and did not find useful 
information on the net. Hints or pointers appreciated,

Horst

-- 
Horst at freedict.de
Horst Eyermann 
Germany

You need a dictionary? - visit http://www.freedict.de
for free (GPL) dictionaries (unix; windows work in progress)
For windows, visit http://www.freedict.de/wbuch

A article (in German) about dictionary efforts on the net
http://www.heise.de/tp/deutsch/inhalt/on/5927/1.html 




More information about the Python-list mailing list