2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ What other tools/kind of tools are you working with except for Mathematica for the purpose of automating? $\endgroup$
    – C. E.
    Oct 21, 2013 at 21:52
  • $\begingroup$ Stuff I'm building myself. Perl scripts and whatnot. $\endgroup$
    – wfaulk
    Oct 22, 2013 at 0:23

1 Answer 1

1
$\begingroup$

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).

$\endgroup$
10
  • $\begingroup$ And how can you be sure that the buffer flushing is done in the right order? $\endgroup$ Oct 21, 2013 at 22:42
  • $\begingroup$ @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. $\endgroup$
    – Hector
    Oct 21, 2013 at 22:50
  • $\begingroup$ No, I mean something worse: How do you know when Mathematica calls the OS to write the file? $\endgroup$ Oct 21, 2013 at 22:51
  • $\begingroup$ @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. $\endgroup$
    – Hector
    Oct 21, 2013 at 22:59
  • 2
    $\begingroup$ All your expectations sound very reasonable, but the OP asked "How can I be sure that Mathematica is done writing the file?" (emphasis mine) :) $\endgroup$ Oct 21, 2013 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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