Setting an exclusive lock on a file

Discussion in 'Python' started by Eugene Perederey, Mar 29, 2009.

  1. 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?

    --
    Sincerely yours, Eugene Perederey
     
    Eugene Perederey, Mar 29, 2009
    #1
    1. Advertising

  2. Eugene Perederey schrieb:
    > 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
     
    Diez B. Roggisch, Mar 29, 2009
    #2
    1. Advertising

  3. Eugene Perederey schrieb:
    > 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
     
    Diez B. Roggisch, Mar 29, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruintje Beer

    Exclusive file lock

    Bruintje Beer, Dec 21, 2007, in forum: Java
    Replies:
    6
    Views:
    504
  2. Steven D'Aprano
    Replies:
    3
    Views:
    1,856
    Jeffrey Straszheim
    Dec 6, 2008
  3. Sea&Gull

    Exclusive lock of a file

    Sea&Gull, Jan 11, 2005, in forum: Ruby
    Replies:
    2
    Views:
    137
    Bill Atkins
    Jan 11, 2005
  4. Replies:
    0
    Views:
    98
  5. lokesh mavale
    Replies:
    1
    Views:
    84
Loading...

Share This Page