How to determine if a file is busy?

Diez B. Roggisch deetsNOSPAM at web.de
Fri Oct 8 07:23:49 EDT 2004


> I have written a stay-open-AppleScript for Mac OS X, gathering certain
> informations from different apps, which can only be gathered via
> AppleScript. This script writes its informations into a simple text
> file, every 15 seconds. And a python script is supposed to read this
> text file on a regular basis in order to feed the aquired datat into a
> database working only on Windows. Now the problem: How can I prevent
> that the python script reads the content of the text file, while my
> AppleScripts writes new information to it? Is there any 'busy'-status
> for files?

You could use a file-lock using the module fcntl.lock operation on the file
descriptor of your file. That ensures that only when the writing is
completed, the reading can occur. Check

man flock

on your console/terminal. But that works only on _one_ machine. When you
have a shared filesystem that two different OSes access, you might be
better off with a protocol of some sort - by that I mean that you use e.g.
http-style header information. So your producer-script gathers data - lets
say 4000 bytes - and writes this to the file:

Content-Length: 4000

<data>

Then your consumer-script reads the header. Only after that is fully
received and understood, it knows how many bytes to expect after the second
newline and won't start processing earlier.

Apart from that, the usecase you describe cries for a decent
inter-process-communication. You could e.g. use XMLRPC, PYRO, CORBA or the
like. That would free you from the necessity of a shared filesystem between
windows and macosx, and from implementing a protocol.

-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list