fcntl problems

mhearne808[insert-at-sign-here]gmail[insert-dot-here]com mhearne808 at gmail.com
Fri Aug 31 09:47:02 EDT 2007


On Aug 31, 12:23 am, Miles <semantic... at gmail.com> wrote:
> Sorry, that last quote-only reply was accidental. :)
>
> On 8/30/07, mhearne808 wrote:
> > I've been doing some experiments, and here are some specific examples
> > to try.
>
> [snipped examples]
>
> > From these last two experiments I can only conclude that file locking
> > isn't doing a durned thing.
>
> > What's going on?
>
> File locking isn't doing a durned thing in those cases because you're
> only obtaining the lock from a single process.
>
> > According to my Python Cookbook:
> > "Exclusive lock: This denies all _other_ processes both read and write
> > access to the file."
>
> This is only for mandatory locking; POSIX flock is advisory locking,
> which states: "Only one process may hold an exclusive lock for a given
> file at a given time."  Advisory locks don't have any effect on
> processes that don't use locks.  Mandatory locks are kernel enforced,
> but non-POSIX and not available in Mac OS X.
>
> -Miles

I think I'm still confused.  Maybe I should explain the behavior that
I want, and then figure out if it is possible.

I have a script that will be run from a cron job once a minute.  One
of the things this script will do is open a file to stash some
temporary results.  I expect that this script will always finish its
work in less than 15 seconds, but I didn't want to depend on that.
Thus I started to look into file locking, which I had hoped I could
use in the following fashion:

Process A opens file foo
Process A locks file foo
Process A takes more than a minute to do its work
Process B wakes up
Process B determines that file foo is locked
Process B quits in disgust
Process A finishes its work

Since I couldn't figure out file locking, I decided to just have
Process A create a "pid" file in the directory - analogous to the
"Occupied" sign on an airplane bathroom.  This works, but it seems a
little hacky.

--Mike




More information about the Python-list mailing list