Python and file locking - NFS or MySQL?

Fredrik Lundh fredrik at
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")

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

where n==1 and m==2.  the 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.


More information about the Python-list mailing list