python 多久刷新一次文件?
- Python 多久刷新一次文件?
- Python 多久刷新一次到标准输出?
我不确定(1)。
至于(2),我相信Python在每一个新行之后都会刷新到stdout。但是,如果您将标准输出重载到文件中,它会经常刷新吗?
- How often does Python flush to a file?
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于文件操作,Python 使用操作系统的默认缓冲,除非您进行其他配置。您可以指定缓冲区大小、无缓冲或行缓冲。
例如,open 函数采用缓冲区大小参数。
http://docs.python.org/library/functions.html#open
“可选的缓冲参数指定文件所需的缓冲区大小:”
代码:
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:
您还可以使用
flush()
方法。我发现这在使用
tail -f
尾随输出文件时很有用。You can also force flush the buffer to a file programmatically with the
flush()
method.I have found this useful when tailing an output file with
tail -f
.您还可以通过调用 io 模块中的只读 DEFAULT_BUFFER_SIZE 属性来检查默认缓冲区大小。
You can also check the default buffer size by calling the read only DEFAULT_BUFFER_SIZE attribute from io module.
我不知道这是否也适用于 python,但我认为这取决于您正在运行的操作系统。
例如,在 Linux 上,输出到终端会在换行符上刷新缓冲区,而对于输出到文件,它仅在缓冲区已满时刷新(默认情况下)。这是因为刷新缓冲区次数较少会更有效,并且用户不太可能注意到输出是否未在文件中的换行符上刷新。
如果您需要的话,您也许可以自动刷新输出。
编辑:我认为你会以这种方式在 python 中自动刷新(基于
来自此处)
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)
这是另一种方法,由 OP 选择他喜欢的方法。
当在
__init__
.py 文件中的任何其他代码之前包含以下代码时,使用print
打印的消息和任何错误将不再记录到 Ableton 的 Log.txt 中,而是单独记录磁盘上的文件:(对于 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 withprint
and any errors will no longer be logged to Ableton's Log.txt but to separate files on your disk:(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