python 多久刷新一次文件?

发布于 2024-09-08 04:27:51 字数 153 浏览 11 评论 0原文

  1. Python 多久刷新一次文件?
  2. Python 多久刷新一次到标准输出?

我不确定(1)。

至于(2),我相信Python在每一个新行之后都会刷新到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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空心空情空意 2024-09-15 04:27:51

对于文件操作,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)
倾城泪 2024-09-15 04:27:51

您还可以使用 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.

烟沫凡尘 2024-09-15 04:27:51

您还可以通过调用 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)
月依秋水 2024-09-15 04:27:51

我不知道这是否也适用于 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()
梦途 2024-09-15 04:27:51

这是另一种方法,由 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文