Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to know when Mathematica is done writing a file? I'm looking to automate something and I'm wanting to have Mathematica put it in a file using Put[], and maybe also Export[]. I need to know that the file is complete before dealing with it outside Mathematica, but I don't want to have to exit Mathematica first.

How can I be sure that Mathematica is done writing the file? Any solution is on the table, from waiting for Mathematica to reprompt, to relying on a metafile. I'd like to avoid writing additional data to the file if at all possible, though.

share|improve this question
    
What other tools/kind of tools are you working with except for Mathematica for the purpose of automating? –  Pickett Oct 21 '13 at 21:52
    
Stuff I'm building myself. Perl scripts and whatnot. –  wfaulk Oct 22 '13 at 0:23

1 Answer 1

If you other tool can wait for the existence of a file, you can use a "lock" file say "C:\Temp\lock.txt"

1) Before Mathematica writes your data, use DeleteFile["C:\\Temp\\lock.txt"] 2) Do all the operations you need. 3) Write one byte to the lock file: 1>>"C:\\Temp\\lock.txt".

In the meantime, your other tool is waiting for lock file to appear. A somewhat better protocol:

1) Before Mathematica writes your data, use FileExistsQ["C:\\Temp\\lock.txt"] to check whether the lock file is there. If so, pause Mathematica for say 10 seconds to give time to the other tool. Loop while the file is there. 2) If the look file has been erased, have Mathematica do the required writing. Once it is done, create the lock file. 3) Rinse and repeat.


My answer above depends on the answer to this question:

Does Mathematica return from a command such as PutAppend without finishing writing?

I think it does not. I have no access to Mathematica's code, but the following test suggests so:

SetDirectory@$TemporaryDirectory;xx = Table[N[Pi, 50000], {10000}]; ByteCount[xx]
Timing[PutAppend[xx, "eraseme.txt"]; FileByteCount["eraseme.txt"]]

226000080

{56.363161, 519927272}

The byte count of the last command is the correct one (as reported by the OS 3 minutes later).

share|improve this answer
    
And how can you be sure that the buffer flushing is done in the right order? –  belisarius Oct 21 '13 at 22:42
    
@belisarius: Are you telling me that there are operative systems that would report the existence of a file after it was erased? or that would not read the latest content of the main file even if it was not flushed to disk? Isn't the job of the operative system to manage buffer flushing? I do not follow your question. –  Hector Oct 21 '13 at 22:50
    
No, I mean something worse: How do you know when Mathematica calls the OS to write the file? –  belisarius Oct 21 '13 at 22:51
    
@belisarius: I expect that Mathematica would not return from the command until the operative system says it can. I do not think that Mathematica should be in the business of caching files. Also, I expect that, unless told otherwise, Mathematica executes commands sequentially ... in the same thread. I might be mistaken in my expectations though. –  Hector Oct 21 '13 at 22:59
2  
All your expectations sound very reasonable, but the OP asked "How can I be sure that Mathematica is done writing the file?" (emphasis mine) :) –  belisarius Oct 21 '13 at 23:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.