Re: fcntl problems
- From: "mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@xxxxxxxxx>
- Date: Fri, 31 Aug 2007 08:14:40 -0700
On Aug 31, 8:42 am, Miles <semantic...@xxxxxxxxx> wrote:
On 8/31/07, mhearne808 wrote:
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
That would look like (untested):
importfcntl, sys
f = open('foo', 'w+')
try:
fcntl.flock(f.fileno(),fcntl.LOCK_EX |fcntl.LOCK_NB)
except IOError, e:
if e.args[0] == 35:
sys.exit(1)
else:
raise
f.seek(0, 2) # seek to end
# do your thing with the file
f.flush()fcntl.flock(f.fileno(),fcntl.LOCK_UN)
f.close()
-Miles
I tested that, and it works! Thanks!
Looking at my flock(3) man page, I'm guessing that "35" is the error
code for EWOULDBLOCK. Which system header file am I supposed to look
in to figure that magic number out?
I would make the argument that this module could be either more
pythonic, or simply documented more completely. The open source
response, of course, would be "go for it!".
--Mike
.
- Follow-Ups:
- Re: fcntl problems
- From: Michael J. Fromberger
- Re: fcntl problems
- From: Miles
- Re: fcntl problems
- References:
- fcntl problems
- From: mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
- Re: fcntl problems
- From: mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
- Re: fcntl problems
- From: Miles
- Re: fcntl problems
- From: mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
- Re: fcntl problems
- From: Miles
- fcntl problems
- Prev by Date: Re: Setting a read-only attribute
- Next by Date: Re: Biased random?
- Previous by thread: Re: fcntl problems
- Next by thread: Re: fcntl problems
- Index(es):
Relevant Pages
|