flock seems very unsafe, python fcntl bug?

X

xucs007

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()
 
J

Jens Henrik Leonhard Jensen

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top