在什么可能的情况下,人们应该更喜欢无缓冲的输出?
通过我的上一个问题中的讨论,我开始知道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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要交互式任务的无缓冲输出。我的意思是,当您期望某人或其他东西对输出做出响应时,您不希望输出卡在某个缓冲区中。
例如,您不希望发送到 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.)
以下是来自 StackOverflow 的 Perl 用户的一小部分样本,他们从学习设置
$| 中受益匪浅。 = 1
:STDOUT 外部重定向且无在控制台看到的输出
当 Sleep() 为使用
无法使用 print 写入文件
perl 附加问题
perl 脚本执行不清楚
Perl:运行“守护进程”并打印
在 Perl 中重定向管道的 STDOUT
为什么我的父进程看不到子进程输出直到退出?
为什么添加或删除换行符会改变此 perl for 循环的功能吗?
Perl 不正确打印
在 Perl 中,如何在输入到达时立即对其进行处理,而不是等待换行符?
保持输出流与屏幕上显示的完全相同(使用交互式数据时)的简单方法是什么?
是否可以从 perl 打印进程退出之前的 CGI?
为什么在每次迭代时不打印输出任何内容当我使用 sleep 时出现循环?
Perl 守护进程不与 Sleep() 一起工作< /a>
Here's a small sample of Perl users from StackOverflow who have benefited from learning to set
$| = 1
:STDOUT redirected externally and no output seen at the console
Perl Print function does not work properly when Sleep() is used
can't write to file using print
perl appending issues
Unclear perl script execution
Perl: Running a "Daemon" and printing
Redirecting STDOUT of a pipe in Perl
Why doesn't my parent process see the child's output until it exits?
Why does adding or removing a newline change the way this perl for loop functions?
Perl not printing properly
In Perl, how do I process input as soon as it arrives, instead of waiting for newline?
What is the simple way to keep the output stream exactly as it shown out on the screen (while interactive data used)?
Is it possible to print from a perl CGI before the process exits?
Why doesn't print output anything on each iteration of a loop when I use sleep?
Perl Daemon Not Working with Sleep()
当通过套接字或管道写入另一个程序时,它会很有用。当您将调试信息写入 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.