I'm unsure about (1).
As for (2), I believe Python flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?
For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.
For example, the open function takes a buffer size argument.
http://docs.python.org/library/functions.html#open
"The optional buffering argument specifies the file’s desired buffer size:"
code:
bufsize = 0 f = open('file.txt', 'w', buffering=bufsize)
You can also force flush the buffer to a file programmatically with the flush()
method.
with open('out.log', 'w+') as f: f.write('output is ') # some work s = 'OK.' f.write(s) f.write('\n') f.flush() # some other work f.write('done\n') f.flush()
I have found this useful when tailing an output file with tail -f
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With