Python and file locking - NFS or MySQL?

Fredrik Lundh fredrik at pythonware.com
Fri Sep 2 15:11:33 CEST 2005


Fredrik Lundh wrote:

> 5) check the number of links to each file
>
>       n = os.stat(tempfile)[3]
>       m = os.stat(lockfile)[3]

aw, forget that.  I shouldn't trust google over my own code.  here's the
correct algorithm:

    f = open(tempfile, "w")
    f.close()

    n = os.stat(tempfile)[3]
    os.link(tempfile, lockfile)
    m = os.stat(tempfile)[3]
    if n == m+1:
        success!

where n==1 and m==2.  the os.link call can be placed in a try/except
clause (if it fails, m won't be 2), or you can use a try/except around the
entire thing.

</F> 






More information about the Python-list mailing list