在什么可能的情况下,人们应该更喜欢无缓冲的输出?

发布于 2024-11-18 09:01:23 字数 338 浏览 2 评论 0原文

通过我的上一个问题中的讨论,我开始知道Perl默认提供行缓冲区输出。

$| = 0; # for buffered output (by default)

如果你想获得无缓冲的输出,那么将特殊变量 $| 设置为 1 即

$| = 1;  # for unbuffered output

现在我想知道在什么情况下人们应该更喜欢无缓冲的输出?

By the discussion in my previous question I came to know that Perl gives line buffer output by default.

$| = 0; # for buffered output (by default)

If you want to get unbuffered output then set the special variable $| to 1 i.e.

$| = 1;  # for unbuffered output

Now I want to know that what can be the possible situations where one should prefer the unbuffered output?

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

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

发布评论

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

评论(3

蒗幽 2024-11-25 09:01:23

您需要交互式任务的无缓冲输出。我的意思是,当您期望某人或其他东西对输出做出响应时,您不希望输出卡在某个缓冲区中。

例如,您不希望发送到 STDOUT 的用户提示被缓冲。 (这就是为什么 STDOUT 在连接到终端时永远不会完全缓冲。它只是行缓冲,并且尝试从 STDIN 读取时会刷新缓冲区。)

例如,您希望通过管道和套接字发送的请求不会被卡住在某个缓冲区中,因为连接的另一端永远不会看到它。


我能想到的唯一另一个原因是,当您不希望在发生不可恢复的错误(例如恐慌或信号死亡)时将重要数据卡在缓冲区中。

例如,您可能希望保持日志文件不缓冲,以便能够诊断严重问题。 (这就是默认情况下不缓冲 STDERR 的原因。)

You want unbuffered output for interactive tasks. By that, I mean you don't want output stuck in some buffer when you expect someone or something else to respond to the output.

For example, you wouldn't want user prompts sent to STDOUT to be buffered. (That's why STDOUT is never fully buffered when attached to a terminal. It is only line buffered, and the buffer is flushed by attempts to read from STDIN.)

For example, you'd want requests sent over pipes and sockets to not get stuck in some buffer, as the other end of the connection would never see it.


The only other reason I can think of is when you don't want important data to be stuck in a buffer in the event of a unrecoverable error such as a panic or death by signal.

For example, you might want to keep a log file unbuffered in order to be able to diagnose serious problems. (This is why STDERR isn't buffered by default.)

坏尐絯℡ 2024-11-25 09:01:23
ま昔日黯然 2024-11-25 09:01:23

当通过套接字或管道写入另一个程序时,它会很有用。当您将调试信息写入 STDOUT 以实时观察程序状态时,它也很有用。

It can be useful when writing to another program over a socket or pipe. It can also be useful when you are writing debugging information to STDOUT to watch the state of your program live.

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