Setting an exclusive lock on a file

E

Eugene Perederey

Hi all,
I want to set an exclusive lock on a file so other processes wouldn't
be able to write to it.
So I write

import fcntl
fd=open('myfile','w')
fcntl.lockf(fd, fcntl.LOCK_EX)
fd.write('some bytes')
fd.close()

But it turns out that my script doesn't write anything to file whereas
other processes do. What's wrong in my code?
 
D

Diez B. Roggisch

Eugene said:
Hi all,
I want to set an exclusive lock on a file so other processes wouldn't
be able to write to it.
So I write

import fcntl
fd=open('myfile','w')
fcntl.lockf(fd, fcntl.LOCK_EX)
fd.write('some bytes')
fd.close()

But it turns out that my script doesn't write anything to file whereas
other processes do. What's wrong in my code?

http://code.activestate.com/recipes/65203/

Diez
 
D

Diez B. Roggisch

Eugene said:
Hi all,
I want to set an exclusive lock on a file so other processes wouldn't
be able to write to it.
So I write

import fcntl
fd=open('myfile','w')
fcntl.lockf(fd, fcntl.LOCK_EX)
fd.write('some bytes')
fd.close()

But it turns out that my script doesn't write anything to file whereas
other processes do. What's wrong in my code?

Sorry, my other answer wasn't complete.

The "problem" with file-locking is that it's AFAIK cooperative. Just
because *you* lock a file doesn't mean anybody else is stopped from
using it.

I'm not sure if there is something else to overcome this.

Diez
 

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

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top