473,769 Members | 5,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine if a file is busy?

Hi,

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?

Thanks in advance from cold and cloudy Germany!

Martin
Jul 18 '05 #1
7 5776
Benjamin Niemann <b.*******@bett ernet.de> wrote:
How about writing the data to a file 'mydata.new' and when you are finished,
delete 'mydata' and rename 'mydata.new' to 'mydata'.
This way the python script will always get 'complete' files (and sometimes no
file at all, when it come in between rm and mv ...)


I believe that 'mv' on Mac OS X, within a normal filesystem, is an
atomic operation anyway -- so, so need to rm first then mv. On the
other hand, this would give no intrinsic guarantee that the datafile
previously produced HAS been consumed/recorded before it's removed.
Alex
Jul 18 '05 #2
On Fri, Oct 08, 2004 at 01:54:03PM +0200, Alex Martelli wrote:
Benjamin Niemann <b.*******@bett ernet.de> wrote:
How about writing the data to a file 'mydata.new' and when you are finished,
delete 'mydata' and rename 'mydata.new' to 'mydata'.
This way the python script will always get 'complete' files (and sometimes no
file at all, when it come in between rm and mv ...)


I believe that 'mv' on Mac OS X, within a normal filesystem, is an
atomic operation anyway -- so, so need to rm first then mv. On the
other hand, this would give no intrinsic guarantee that the datafile
previously produced HAS been consumed/recorded before it's removed.

Then the consumer of that file should remove it afterwards.

Andreas
Jul 18 '05 #3
Andreas Kostyrka <an*****@kostyr ka.org> wrote:
On Fri, Oct 08, 2004 at 01:54:03PM +0200, Alex Martelli wrote:
Benjamin Niemann <b.*******@bett ernet.de> wrote:
How about writing the data to a file 'mydata.new' and when you are
finished, delete 'mydata' and rename 'mydata.new' to 'mydata'. This
way the python script will always get 'complete' files (and sometimes
no file at all, when it come in between rm and mv ...)


I believe that 'mv' on Mac OS X, within a normal filesystem, is an
atomic operation anyway -- so, so need to rm first then mv. On the
other hand, this would give no intrinsic guarantee that the datafile
previously produced HAS been consumed/recorded before it's removed.

Then the consumer of that file should remove it afterwards.


Possibly, but that, per se, doesn't mean a file can't get deleted
without ever having been used. If the producer must wait for the
deletion (not necessarily easy in AppleScript, I believe) then the
producer could be stalled by the implied 1-length 'queue'...

I'm not sure about the best solution because I'm unsure about the exact
abilities of AppleScript, where the Python script is running (on Windows
with a [EEK] SMB share to the Mac, or on the Mac itself), etc...
Alex
Jul 18 '05 #4
Alex Martelli wrote:
Andreas Kostyrka <an*****@kostyr ka.org> wrote:

On Fri, Oct 08, 2004 at 01:54:03PM +0200, Alex Martelli wrote:
Benjamin Niemann <b.*******@bett ernet.de> wrote:
How about writing the data to a file 'mydata.new' and when you are
finished, delete 'mydata' and rename 'mydata.new' to 'mydata'. This
way the python script will always get 'complete' files (and sometimes
no file at all, when it come in between rm and mv ...)

I believe that 'mv' on Mac OS X, within a normal filesystem, is an
atomic operation anyway -- so, so need to rm first then mv. On the
other hand, this would give no intrinsic guarantee that the datafile
previously produced HAS been consumed/recorded before it's removed.


Then the consumer of that file should remove it afterwards.

Possibly, but that, per se, doesn't mean a file can't get deleted
without ever having been used. If the producer must wait for the
deletion (not necessarily easy in AppleScript, I believe) then the
producer could be stalled by the implied 1-length 'queue'...

I'm not sure about the best solution because I'm unsure about the exact
abilities of AppleScript, where the Python script is running (on Windows
with a [EEK] SMB share to the Mac, or on the Mac itself), etc...


The AppleScript could rename the .new file to .[timestamp], producing a new file
for each iteration. The Python script would consume & delete all .[timestamp]
files in lexical order, but must ignore the .new file if it happens to be there.
Jul 18 '05 #5

"Alex Martelli" <al*****@yahoo. com> schrieb im Newsbeitrag
news:1glcbu7.po 1w0e8mxyl6N%al* ****@yahoo.com. ..
Benjamin Niemann <b.*******@bett ernet.de> wrote:
How about writing the data to a file 'mydata.new' and when you are finished, delete 'mydata' and rename 'mydata.new' to 'mydata'.
This way the python script will always get 'complete' files (and sometimes no file at all, when it come in between rm and mv ...)


I believe that 'mv' on Mac OS X, within a normal filesystem, is an
atomic operation anyway -- so, so need to rm first then mv. On the
other hand, this would give no intrinsic guarantee that the datafile
previously produced HAS been consumed/recorded before it's removed.
Alex


First of all: Thanks so much for your help! I am really astonished about
everybody doing his best to find a solution for my problem.

It might sound funny, but it is not important, that a specific data file has
already been consumed. For me, it is only important, that my Python script
does not open & read a file, which is currently in use by my AppleScript.
It's more like a webcam thing: If a picture taken by the cam, is not put
into the album/webserver, that is no problem, because there will be other
pictures taken by the cam in the future.

The platform independence of Python is great for me as I can fetch the
informations from a Windows XP PC and a Mac to feed the database running in
our electroplating company, but I am also happy to program all the rest of
the application in Python, because as much as I do like AppleScript (Studio)
for its power on the Mac, I hate writing its longish code leading to big
scripts doing little things...Python is just so snappy ;-) (for someone who
has coded a long time in AppleScript)

I am now trying to implement something like a temp file, which I will
finally (after writing all the informations into it) copy into an export
location, where the Python script can fetch it any time. My only concern is,
if copying is also a problem (Python script reads the text file, while the
copy process is not yet finalized...).. ..

Thanks so much once again, you are a great community, Python is a great
lannguage!

Martin
Jul 18 '05 #6
Benjamin Niemann <b.*******@bett ernet.de> wrote:
...
The AppleScript could rename the .new file to .[timestamp], producing a
new file for each iteration. The Python script would consume & delete all
.[timestamp] files in lexical order, but must ignore the .new file if it
happens to be there.


Sounds good to me, placing minimal demands on the AppleScript's
abilities (surely getting a timestamp and doing a rename is well within
its powers!) _and_ on the filesystem used for communication (surely this
small subset of "atomicity of renames" -- which appears to be the only
issue here -- can be guaranteed even by SMB).
Alex
Jul 18 '05 #7
Martin Michel <bu*****@t-online.de> wrote:
...
I am now trying to implement something like a temp file, which I will
finally (after writing all the informations into it) copy into an export
location, where the Python script can fetch it any time. My only concern is,
if copying is also a problem (Python script reads the text file, while the
copy process is not yet finalized...).. ..


Renaming, as suggested on the other subthread, sounds preferable to
copying, more likely to be an atomic operation on any kind of shared
filesystem. Your applescript should write 'foobar.new' (either directly
in the export location, or copying it there each time it's ready) then
rename it to something like 'foobar.ready_2 0041008_171921' or other
unique name that's easily identifiable (this example uses a timestamp in
an order Y-M-D-h-m-s that will automatically "sort right").

Python can periodically look for 'foobar.ready_* ' files in the export
location, and consume and delete them -- if it finds more than one it's
up to you whether it should consume all or just the newest one, but all
should be deleted anyway, whether they're consumed or otherwise ignored.

This protocol appears to me to be quite safe, with minimal demands on
the filesystem you're sharing.
Alex
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
39485
by: Sami Viitanen | last post by:
Hello, How can I check if a file is binary or text? There was some easy way but I forgot it.. Thanks in adv.
1
1285
by: Asra | last post by:
Hello, I have a frame's onload event calling a function which needs to know which file was loaded. Is there any java object/method to determine the file name or any type of file id? Thanks, Asra
10
10100
by: Marc Jennings | last post by:
Hi there, Can anyone point out any really obvious flaws in the methodology below to determine the likely encoding of a file, please? I know the number of types of encoding is small, but that is only because the possibilities I need to work with is a small list. > private string determineFileEncoding(FileStream strm) > { > long originalSize = strm.Length;
8
9810
by: Dinesh Jain | last post by:
Hi all, I encountered a serious problem while working with clipboard class in VB.NET. I want to simulate cut-copy-paste operation with my application and Windows Explorer. User can copy files from Explorer and paste it into my application & vice-a-versa. My question is- How can I determine if user has copied or cut the files from Windows Explorer? I want to differentiate cut & copy.
15
6403
by: Snedker | last post by:
I'm using (HttpWebRequest and HttpWebResponse to check for updates. But how do I determine the size of the file before download? What I have in mind is a status text like "You have downloaded xxxx of yyyy bytes" I'm trying to find the y's within this: ---
3
2435
by: Mark Gibson | last post by:
Is there an equivalent to the unix 'file' command? $ file min.txt min.txt: ASCII text $ file trunk trunk: directory $ file compliance.tgz compliance.tgz: gzip compressed data, from Unix What I really want to do is determine if a file is 1) a directory, 2) a
0
987
by: yxq | last post by:
Hello, I found a command line tool that verfiy file digital signature, it can determine that a file signed by Microsoft. The download is below. http://www.kztechs.com/filedsv/filedsv.zip My question: How to realize this function in VB.NET, if it is impossible, where can buy or download a free ActiveX for using in VB.NET? Thank you very much.
6
3246
by: John | last post by:
Hi I am emailing a text file as attachment via the below code. The problem is that after the file has been emailed, it becomes locked and can not be accessed by further code such as IO.File.AppendAllText(FileName, Content) which gives the error 'file is in use by another process' or something similar. How can I release the file after it has been emailed or email it in a way that it does not get locked? Many Thanks
0
902
by: Dist | last post by:
Hi, I would just like to know if there is a way to determine if a specific file is being copied in windows using .net. For example, I got a service running in the background to check all files, and copy their names to a file, if they are being copied from anywhere to anywhere.
3
2072
by: Steph | last post by:
hello, i ve a probleme when deleting a directory and when i want create file immediatly after. 1) Directory.Delete(myPath, true); 2) TextWriter sw = new StreamWriter(myPath +"test.aspx"); i obtain Exception Details: System.UnauthorizedAccessException: Access to the path '***' is denied.
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.