CGIs and file exclusion

Michael Foord fuzzyman at gmail.com
Fri Nov 5 05:28:55 EST 2004


"darkhorse" <not_my_address at terra.es> wrote in message news:<2uvedvF2g1pv3U1 at uni-berlin.de>...
> Hi all,
> 
> While doing a quite big "set of programs" for a university subject I've
> found myself in the middle of a problem someone surely has had before, so
> I'm looking for some help.
> 
> At one point, I call a python cgi that pickle.load's a file, adds or deletes
> a registry and dumps the result again in the file.
> I'm worried that the cgi could be called simultaneously from two or more
> different computers, thus most probably corrupting the files. I don't think
> I can use a mutex as it's two different instances of the program and not
> different threads, so I was thinking about locking the files between
> programs, but I really don't know how to do that. It's not a problem if
> there's no portable way of doing this, it's only going to be run on a linux
> based computer.
> Another solution I would accept is that the second called cgi detects that
> other instance is running and displays a message saying to try again later.
> Yes, not quite professional, but it'd do the job, after all this is just a
> little detail I want to settle for a quite picky professor and not a "real
> life" thing.
> I think that's all the background you need, if someone can answer with
> references on what should I look for or even better example code that would
> be simply great.
> Many thanks in advance.
> DH

A simple solution that doesn't scale well is to create a file when the
access starts. You can check if the file exists and pause until the
other process deletes it - with a timeout in case the file gets keft
there due to an error.

Obviously not an industrial strength solution, but it does work...

import time
import os

def sleep(thelockfile, sleepcycle=0.01, MAXCOUNT=200):
    """Sleep until the lockfile has been removed or a certain number
of cycles have gone.
    Defaults to a max 2 second delay.
    """
    counter = 0
    while os.path.exists(thelockfile):
        time.sleep(sleepcycle)
        counter += 1
        if counter > MAXCOUNT: break
        
def createlock(thelockfile):
    """Creates a lockfile from the path supplied."""
    open(thelockfile, 'w').close()
    
def releaselock(thelockfile):
    """Deletes the lockfile."""
    if os.path.isfile(thelockfile):
        os.remove(thelockfile)

The sleep function waits until the specified file dissapears - or it
times out.

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html



More information about the Python-list mailing list