How to lock files (the easiest/best way)?
Fuzzyman
fuzzyman at gmail.com
Mon Jul 17 15:23:36 EDT 2006
Sybren Stuvel wrote:
> Elmo Mäntynen enlightened us with:
> > Only locally. I want to be able to read/write to a single file from
> > multiple possibly parallel processes. Would 'touch lock' (or
> > something like that) work reliably (this just occured to me)?
>
> I use a lock directory for that, os.mkdir('/var/lock/somedir').
> If you use a file, you need two steps:
> 1) Check whether the lock-file exists
> 2) Create the lock-file
>
> This is not atomic. With a directory, creating it will fail if it
> already exists. This means you can atomically check for the lock, and
> if it doesn't exist already, you've immediately created it too.
`Pathutils <http://www.voidspace.org.uk/python/pathutils.html>`_ has a
simple interface to a system like this.
Apparently not all platforms guarantee that attempts to create a
directory will fail if the directory already exists - so pathutils does
*slightly* more, but it's the same idea.
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
>
> Sybren
> --
> The problem with the world is stupidity. Not saying there should be a
> capital punishment for stupidity, but why don't we just take the
> safety labels off of everything and let the problem solve itself?
> Frank Zappa
More information about the Python-list
mailing list