[fcntl]how to lock a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marcello

    #1

    [fcntl]how to lock a file

    Hello
    I need to do this:
    1 opening a file for writing/appending
    2 to lock the file as for writing (i mean: the program
    that lock can keep writing, all others programs can't )
    3 wtite and close/unlock
    Even better would be changing the order of steps 1 and 2 (that
    is,first locking than writing, but it seems to me that in order to block
    i need the id of the file...)

    Untill now i have tried to do smthing like :
    import fcntl
    f=open("prova.t xt", 'w')
    fcntl.flock(f.f ileno(),fcntl.L OCK_EX)
    f.write("aaa")

    #meanwhile i open prova.txt with an editor in another window
    #and write in "bbb" (without getting any error/warning)and then follow
    #in the python shell with

    f.close()

    But then opening the file i can see "bbb" instead of "aaa"
    I'm wandering, am i completely lost(misled?)Am i waiting for fcntl to do
    what it doesn't?

    I have tried too with:

    f1=os.open('pro va.txt',os.O_EX CL|os.O_APPEND)
    os.write(f1,"ql cosa")
    that should solve the problem of the order of the step, but i not even
    can write from the python shell with that.

    python 2.2
    linux
    kernel 2.4
    glibc-2.3.2-95.6

    Thanks in advance for any help.
    sorry for my bad english
    Grazie
    Marcello
  • Eric S. Johansson

    #2
    Re: [fcntl]how to lock a file

    marcello wrote:[color=blue]
    > Hello
    > I need to do this:
    > 1 opening a file for writing/appending
    > 2 to lock the file as for writing (i mean: the program
    > that lock can keep writing, all others programs can't )
    > 3 wtite and close/unlock[/color]



    been using it for years and it makes locking really simple. I have no
    idea why the main Python distribution is missing such an important piece
    of functionality.

    Comment

    • marcello

      #3
      Re: [fcntl]how to lock a file

      thank you a lot for your answering
      Bye
      Marcello

      Comment

      • Carl J. Van Arsdall

        #4
        Re: [fcntl]how to lock a file

        You know, I spent some time with this module and had some interesting
        results. I had to do file locking over nfs using lockd. This poised an
        interested problem as I couldn't get the python fcntl module to work
        correctly via nfs even with lockd setup. Anyhow, I ended up writing an
        extention in C and somehow this called fcntl differently and I had no
        issues.

        If you end up having problems working with the python fcntl module let
        me know your configuration I'd be interested to see if anyone else had
        similar problems to me.

        -c

        marcello wrote:[color=blue]
        > Hello
        > I need to do this:
        > 1 opening a file for writing/appending
        > 2 to lock the file as for writing (i mean: the program
        > that lock can keep writing, all others programs can't )
        > 3 wtite and close/unlock
        > Even better would be changing the order of steps 1 and 2 (that
        > is,first locking than writing, but it seems to me that in order to block
        > i need the id of the file...)
        >
        > Untill now i have tried to do smthing like :
        > import fcntl
        > f=open("prova.t xt", 'w')
        > fcntl.flock(f.f ileno(),fcntl.L OCK_EX)
        > f.write("aaa")
        >
        > #meanwhile i open prova.txt with an editor in another window
        > #and write in "bbb" (without getting any error/warning)and then follow
        > #in the python shell with
        >
        > f.close()
        >
        > But then opening the file i can see "bbb" instead of "aaa"
        > I'm wandering, am i completely lost(misled?)Am i waiting for fcntl to do
        > what it doesn't?
        >
        > I have tried too with:
        >
        > f1=os.open('pro va.txt',os.O_EX CL|os.O_APPEND)
        > os.write(f1,"ql cosa")
        > that should solve the problem of the order of the step, but i not even
        > can write from the python shell with that.
        >
        > python 2.2
        > linux
        > kernel 2.4
        > glibc-2.3.2-95.6
        >
        > Thanks in advance for any help.
        > sorry for my bad english
        > Grazie
        > Marcello
        >[/color]


        --

        Carl J. Van Arsdall
        cvanarsdall@mvi sta.com
        Build and Release
        MontaVista Software

        Comment

        • marcello

          #5
          Re: [fcntl]how to lock a file

          Carl J. Van Arsdall wrote:[color=blue]
          > [...]
          >
          > If you end up having problems working with the python fcntl module let
          > me know your configuration I'd be interested to see if anyone else had
          > similar problems to me.[/color]


          Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
          [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
          kernel 2.4.21-4.0.1.ELsmp
          glibc-2.3.2-95.6



          What else can i add?

          best regards
          Marcello

          Comment

          • Donn Cave

            #6
            Re: [fcntl]how to lock a file

            In article <e1gf6o$74h$1@e mma.aioe.org>, marcello <marcs2006@gmai l.com>
            wrote:
            [color=blue]
            > Carl J. Van Arsdall wrote:[color=green]
            > > [...]
            > >
            > > If you end up having problems working with the python fcntl module let
            > > me know your configuration I'd be interested to see if anyone else had
            > > similar problems to me.[/color]
            >
            >
            > Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
            > [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
            > kernel 2.4.21-4.0.1.ELsmp
            > glibc-2.3.2-95.6
            >
            >
            >
            > What else can i add?[/color]

            Maybe something about the problem?

            There are four functions in that module - fcntl, flock,
            ioctl, lockf. Which one were you using? What were you
            trying to do with it?

            Did it raise an exception? Returned but didn't effectively
            interlock against other processes -- on the same host, on
            other hosts? Failed to return when the file should have
            been OK to lock?

            Is it possible that locks would work fine in a test program,
            but fail in your application due to brain damaged POSIX
            filesystem locking semantics?

            We can start with stuff you probably already know.

            - The Berkeley flock(2) function is great but is not
            supported by NFS.

            - fcntl(2) F_SETLK etc. are supported by NFS, but have
            extremely inconvenient semantics, part of the POSIX
            1003.1 specifications, particularly the part about losing
            a lock on ANY close(), even if the file descriptor remains
            open for fcntl(2)'s caller.

            - lockf(3) is fcntl(2).

            - Some platforms provide an flock() that is actually
            fcntl(2), can't think of the right words to express how
            deplorable that practice is.

            - On the rare platform that doesn't do this - where they
            are honest enough to just admit that they don't support
            flock() - Python implements fcntl.flock() with fcntl(2),
            I guess on the theory that you can't have enough brain
            damage. The worst case would be if Python's configure
            missed a bona fide flock(2), which is unlikely but may
            be worth checking if you use flock(2) for a reason.

            Donn Cave, donn@u.washingt on.edu

            Comment

            Working...