File locking

Thomas Wouters thomas at xs4all.net
Tue Apr 24 11:54:06 EDT 2001


On Tue, Apr 24, 2001 at 03:02:36PM +0000, Martin Kaufmann wrote:
> Could somebody help me? I have a script that writes its output in a
> logfile. This logfile is needed to check whether a task has already be
> done. Several workstations are now using the same script (via nfs). So I
> implemented the following file locking mechanism:
> 
>         logfile = posixfile.open(LOGFILE, 'a')
>         logfile.lock('|w')
> 	[code snipped...]
>         log_string = '%s  %s%s  %d  %s  %s\n' % (mytime, host, url,
> 					error_code, message, hostname)
>         logfile.write(log_string)
>         logfile.lock('u')
>         logfile.close()
> 
> But I still get problems with two processes trying to write at the same
> time. What is wrong with my implementation?

NFS and fnctl/flock type locking do not go well together; see this recent
posting of mine on python-dev for a longer explanation:
http://mail.python.org/pipermail/python-dev/2001-April/014267.html

If you're serious about locking over NFS, take a look at Mailman's LockFile
module, which uses the 'link/stat' lock method which used to be in the
'open' manpage of several Linux distributions. We've used this lock method
with BSDI, Linux and Network Appliance NFS servers, and BSDI, FreeBSD,
Linux, SunOS and Solaris clients. It should work on all compliant NFS
clients (which, granted, doesn't say that much -- but our experiences so far
have been good.)

You can find Mailman on www.list.org, or use the cvsweb running on
Sourceforge to grab just LockFile.py.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list