CGIs and file exclusion

Mike Meyer mwm at mired.org
Fri Nov 5 18:00:42 EST 2004


fuzzyman at gmail.com (Michael Foord) writes:

> "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...

To strengthen the solution, write the process id of the script
(available via os.getpid()) to the file. If the file doesn't vanish before
your timeout, you can check to see if the process is still around, and
kill it.

     <mike

> 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:
      f = file(thelockfile)
      pid = int(f.read())
      f.close()
      p = os.popen("/bin/ps -p %s" % pid)
      l = p.read().split('\n')
      p.close()
      if len(l) > 2:
          os.kill(pid, 9)
      

> def createlock(thelockfile):
>     """Creates a lockfile from the path supplied."""
      f = file(thelockfile, 'w')
      f.write(str(os.getpid()))
      f.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

-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list