flock seems very unsafe, python fcntl bug?

Jens Henrik Leonhard Jensen jhlj at statsbiblioteket.dk
Sun Jun 22 05:17:18 EDT 2008


Your problem is that open(...,'w') is not locked.

Use something like:
lockf = open('aaa', 'a')
fnctl.flock(lockf,fnctl.LOCK_EX)
file = open('aaa', 'w')
file.write('asdf')
file.close()
lockf.close()

xucs007 at gmail.com wrote:
> I ran following 2 programs (lock1, lock2) at almost same time,
> to write either "123456", or "222" to file "aaa" at the same time.
> But I often just got "222456" in "aaa" .
> 
> Is this a bug of python fcntl module ? See 2 programs I ran:
> 
> 
> 
> #!/usr/bin/env python
> import fcntl, time
> file = open('aaa', "w")
> fcntl.flock(file, fcntl.LOCK_EX)
> file.write('123456')
> time.sleep(10)
> file.close()
> 
> 
> #!/usr/bin/env python
> import fcntl, time
> file = open('aaa', "w")
> fcntl.flock(file, fcntl.LOCK_EX)
> file.write('222')
> time.sleep(10)
> file.close()



More information about the Python-list mailing list