CGIs and file exclusion

David M. Cooke cookedm+news at physics.mcmaster.ca
Thu Nov 4 18:50:45 EST 2004


"darkhorse" <not_my_address at terra.es> writes:

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

You want the fcntl module, and use either flock() or lockf().

import fcntl, cPickle
# open in update mode
fo = open('somefile.pickle', 'r+')
# acquire an exclusive lock; any other process trying to acquire this
# will block
fcntl.lockf(fo.fileno(), fcntl.LOCK_EX)
data = cPickle.load(fo)
... do stuff to data ...
# go back to the beginning
fo.seek(0)
# write out the new pickle
cPickle.dump(data, fo)
# throw the rest of the (old) pickle away
fo.truncate()
# and close. This releases the lock.
fo.close()

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

If you want to do something like that, use
fcntl.LOCK_EX | fcntl.LOCK_NB as the flags to fcntl.lockf(). If the
lock can't be acquired, an IOError will be raised, and you can print
out your message.

Note that lockf() does advisory locks; a process that just opens the
file and does something with it without calling lockf() *won't* be blocked.

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list