fcntl problems

Hrvoje Niksic hniksic at xemacs.org
Fri Aug 31 16:26:05 CEST 2007


"mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808 at gmail.com> writes:

> I think I'm still confused.

What Miles tried to tell you is that you should call fcnt.flock from
both PA and PB.  In the example you posted, you failed to call it from
PB.  No lock call, so no locking happened.

> 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

File locking supports that scenario, as you suspected.  You need to
use flock with LOCK_EX|LOCK_NB.  If the call succeeds, you got the
lock.  If you get an exception whose errno is EWOULDBLOCK, you quit in
disgust.



More information about the Python-list mailing list