Unclosed files in julia write different output?

I have a julia program that simulates an ensemble of systems. Basically, in each implementation, I write observables as a function of time and write, for the implementation, the results to a file.

I recently noticed that I have a file where I am writing my results that do not have the correct one

close(filename)

      

teams. To be sure, will there be any mistake in my numerical models if I don't close the files I'm using for writing correctly?

Since julia does not throw a compile-time error, I would like to know if it will be done implicitly Julia

+3


source to share


2 answers


The file will be automatically closed at the end of the script (or, if opened in a function, at the end of the function).

More precisely, it will be closed when the file pointer goes out of scope.



More precisely, it will be closed when the file pointer is garbage collected.

+2


source


Everyone IOStream

will be closed and their buffers will be flushed before the Julia process finishes. However, the time at which it will be closed IOStream

is unpredictable.

The exact mechanism is as follows. The function open

that creates the file IOStream

registers a finalizer with a garbage collector. Periodically, Julia invokes the garbage collector, which destroys any unreachable data structures; if it destroys IOStream

, it runs the associated finalizer, which closes the file.



Since the time at which the garbage collector starts is unpredictable, it's best to explicitly close it IOStream

yourself: you should consider the finalization mechanism as just an extra safety measure in case you forget to call close

.

+1


source







All Articles