Re: fcntl problems



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

.



Relevant Pages

  • Re: fcntl problems
    ... of the things this script will do is open a file to stash some ... I expect that this script will always finish its ... Process A opens file foo ...
    (comp.lang.python)
  • Re: Iptables not starting
    ... I have a sample configuration script. ... > $ file foo ... ASCII text, with CRLF line terminators ... the script needed to be in unix format. ...
    (linux.redhat)
  • Re: How to fire an event
    ... There is no script code, I have a check box column and I loop thru the rows, ... manipulating the grid in script would cause a server event to be raised. ... The problem here is that I have no idea whether the grid will raise the ... void gd_UpdateRowBatch ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Outlook Script: How to get the result of an action
    ... Yes I that if save fails it raise an error, but in my script how can I know ... if the save was successfull or not ... If the Save method fails it should raise an error. ...
    (microsoft.public.outlook.program_vba)
  • Re: Error Exception if script doesnt run
    ... #puts e.backtrace.first -> prints out correctly ... I don't get the line number of the script ... raise "Hell" ... where the raise occurred. ...
    (comp.lang.ruby)