What happens if I abruptly close my script while it is still doing file I / O?

here is my question: I am writing a script to check if my site is working fine, the main idea is to get server response time and similar things every 5 minutes or so, and the script will log information every time after checking the server status. I know that you should not close the script while it is in the middle of checks / logs, but I'm curious if there are many servers to check, and also you have to do the input / output file quite often, which will happen if you close the script abruptly ?

OK, here is an example:

while True:
    DemoFile = open("DemoFile.txt", "a")
    DemoFile.write("This is a test!")
    DemoFile.close()

    time.sleep(30)

If I accidentally close the script while this line is DemoFile.write("This is a test!")running, what will I get in DemoFile.txt? Am I getting "This i" (an incomplete line) or a full line or a line not even added?

Hope someone knows the answer.

+3
source share
1 answer

According to python io documentation, buffering is handled according to the buffering parameter for an open function.

The default behavior in this case will be either the block size of the device, or io.DEFAULT_BUFFER_SIZE if the block size cannot be determined. This is probably about 4096 bytes.

, . - , , . flush().

( 0 , " i". , )

@sven, python . , .

+2

Source: https://habr.com/ru/post/1783869/


All Articles