How often does python flush to a file?

后端 未结 5 1863
礼貌的吻别
礼貌的吻别 2020-11-22 04:33
  1. How often does Python flush to a file?
  2. How often does Python flush to stdout?

I\'m unsure about (1).

As for (2), I believe Python flush

相关标签:
5条回答
  • 2020-11-22 05:18

    I don't know if this applies to python as well, but I think it depends on the operating system that you are running.

    On Linux for example, output to terminal flushes the buffer on a newline, whereas for output to files it only flushes when the buffer is full (by default). This is because it is more efficient to flush the buffer fewer times, and the user is less likely to notice if the output is not flushed on a newline in a file.

    You might be able to auto-flush the output if that is what you need.

    EDIT: I think you would auto-flush in python this way (based from here)

    #0 means there is no buffer, so all output
    #will be auto-flushed
    fsock = open('out.log', 'w', 0)
    sys.stdout = fsock
    #do whatever
    fsock.close()
    
    0 讨论(0)
  • 2020-11-22 05:22

    You can also check the default buffer size by calling the read only DEFAULT_BUFFER_SIZE attribute from io module.

    import io
    print (io.DEFAULT_BUFFER_SIZE)
    
    0 讨论(0)
  • 2020-11-22 05:29

    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.

    0 讨论(0)
  • 2020-11-22 05:33

    Here is another approach, up to the OP to choose which one he prefers.

    When including the code below in the __init__.py file before any other code, messages printed with print and any errors will no longer be logged to Ableton's Log.txt but to separate files on your disk:

    import sys
    
    path = "/Users/#username#"
    
    errorLog = open(path + "/stderr.txt", "w", 1)
    errorLog.write("---Starting Error Log---\n")
    sys.stderr = errorLog
    stdoutLog = open(path + "/stdout.txt", "w", 1)
    stdoutLog.write("---Starting Standard Out Log---\n")
    sys.stdout = stdoutLog
    

    (for Mac, change #username# to the name of your user folder. On Windows the path to your user folder will have a different format)

    When you open the files in a text editor that refreshes its content when the file on disk is changed (example for Mac: TextEdit does not but TextWrangler does), you will see the logs being updated in real-time.

    Credits: this code was copied mostly from the liveAPI control surface scripts by Nathan Ramella

    0 讨论(0)
  • 2020-11-22 05:35

    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:"

    • 0 means unbuffered,
    • 1 means line buffered,
    • any other positive value means use a buffer of (approximately) that size.
    • A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files.
    • If omitted, the system default is used.

    code:

    bufsize = 0
    f = open('file.txt', 'w', buffering=bufsize)
    
    0 讨论(0)
提交回复
热议问题