python多久刷新一次文件?

问题:python多久刷新一次文件?

  1. Python多久刷新一次文件?
  2. Python多久刷新一次到stdout?

我不确定(1)。

至于(2),我相信Python每隔一行都会刷新到stdout。但是,如果将stdout重载为文件,它是否会经常刷新?

  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 flushes to stdout after every new line. But, if you overload stdout to be to a file, does it flush as often?


回答 0

对于文件操作,Python使用操作系统的默认缓冲,除非您对其进行配置,否则进行配置。您可以指定缓冲区大小,无缓冲或行缓冲。

例如,open函数采用缓冲区大小参数。

http://docs.python.org/library/functions.html#open

“可选的缓冲参数指定文件的所需缓冲区大小:”

  • 0表示未缓冲,
  • 1表示行缓冲,
  • 任何其他正值表示使用(大约)该大小的缓冲区。
  • 负缓冲意味着使用系统默认值,通常对tty设备使用行缓冲,而对于其他文件则使用完全缓冲。
  • 如果省略,则使用系统默认值。

码:

bufsize = 0
f = open('file.txt', 'w', buffering=bufsize)

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)

回答 1

您也可以使用该flush()方法以编程方式强制将缓冲区刷新到文件。

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

我发现当尾随输出文件时,这很有用tail -f

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.


回答 2

我不知道这是否也适用于python,但我认为这取决于您所运行的操作系统。

例如,在Linux上,输出到终端会在换行符上刷新缓冲区,而输出到文件只会在缓冲区已满时刷新(默认情况下)。这是因为较少次数刷新缓冲区更有效,并且用户不太可能注意到输出是否未刷新到文件中的换行符上。

如果需要,您也许可以自动刷新输出。

编辑:我认为您将以这种方式在python中自动刷新(从此处开始

#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()

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

回答 3

您还可以通过调用io模块中的只读DEFAULT_BUFFER_SIZE属性来检查默认缓冲区大小。

import io
print (io.DEFAULT_BUFFER_SIZE)

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)

回答 4

这是另一种方法,由OP来选择他更喜欢哪个。

如果将以下代码包含在__init__.py文件中,然后再包含其他任何代码,则打印有消息print和任何错误的消息将不再记录到Ableton的Log.txt中,而是将磁盘上的文件分开:

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

(对于Mac,更改#username#为用户文件夹的名称。在Windows上,用户文件夹的路径将具有不同的格式)

当您在文本编辑器中打开文件时,该文件在更改磁盘上的文件时会刷新其内容(例如,Mac:TextEdit不会,但TextWrangler会),您会看到日志实时更新。

鸣谢:这段代码主要是Nathan Ramella从liveAPI控制界面脚本复制的

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