为什么我的perl守护程序无法打印? [英] Why won't my perl daemon print?

查看:93
本文介绍了为什么我的perl守护程序无法打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试守护程序,并且尝试使用 print 语句将信息输出到终端。我的代码的要旨是:

I am debugging a daemon and I'm trying to use print statements to output information to the terminal. The gist of my code is:

#!/usr/bin/env perl

use strict;
use warnings;

use Readonly;

Readonly my $TIMEOUT => ...;

...

while (1) {

   print "DEBUG INFO";

   ...

   sleep $TIMEOUT;
}

但是,没有输出输出到我的终端。为什么这样?

However, no output it getting printed to my terminal. Why is this?

推荐答案

摘要:



使用 $ | = 1 或在打印中添加换行符 \n

Summary:

Use $| = 1 or add a newline, "\n" to the print.

未打印到终端的原因是因为perl缓冲了输出以提高效率。一旦打印缓冲区装满,它将被刷新,并且输出将出现在您的终端中。您可能需要强制刷新缓冲区,因为根据 $ TIMEOUT 的长度,您可能要等待相当长的时间才能输出!

The reason this isn't printing to the terminal is because perl is buffering the output for efficiency. Once the print buffer has been filled it will be flushed and the output will appear in your terminal. It may be desirable for you to force flushing the buffer, as depending on the length of $TIMEOUT you could be waiting for a considerable length of time for output!

有两种清除缓冲区的主要方法:

There are two main approaches to flushing the buffer:

1)在打印到终端时,然后是文件句柄最有可能是 STDOUT 。默认情况下,连接到终端的任何文件句柄都处于 line-buffered 模式,我们可以通过在 print 语句:

1) As you're printing to your terminal, then your filehandle is most likely STDOUT. Any file handles attached to the terminal are by default in line-buffered mode, and we can flush the buffer and force output by adding a newline character to your print statement:

while (1) {
    print "DEBUG INFO\n";
    ...
    sleep $TIMEOUT;
 }

2)第二种方法是使用 $ | 设置为非零时当前文件句柄(默认为 STDOUT 或最后一个 select ed) hot 强制立即刷新缓冲区。因此,以下内容也将强制打印调试信息:

2) The second approach is to use $| which when set to non-zero makes the current filehandle (STDOUT by default or the last to be selected) hot and forces a flush of the buffer immediately. Therefore, the following will also force printing of the debug information:

$| = 1;
while (1) {
    print "DEBUG INFO";
    ...
    sleep $TIMEOUT;
}

如果使用这样的语法令人困惑,那么您可能要考虑:

If using syntax such as this is confusing, then you may like to consider:

use IO::Handle;
STDOUT->autoflush(1);

while (1) {
    print "DEBUG INFO";
    ...
    sleep $TIMEOUT;
}

在许多需要立即刷新缓冲区的代码示例中,您可能会看到 $ | ++ 用于使文件句柄变热并立即刷新缓冲区,而-$ | 使文件句柄变冷并关闭自动冲洗功能。有关更多详细信息,请参见以下两个答案:

In many code examples where immediate flushing of the buffer is required, you may see $|++ used to make a file-handle hot and immediately flush the buffer, and --$| to make a file-handle cold and switch off auto-flushing. See these two answers for more details:

  • Perl operator: $|++; dollar sign pipe plus plus
  • How does --$| work in Perl?

如果您想了解有关Perl缓冲区的更多信息,那么我建议阅读遭受缓冲,它可以很好地了解为什么我们要缓冲并说明如何打开缓冲和关闭。

If you're interested in learning more about perl buffers, then I would suggest reading Suffering from Buffering, which gives great insight into why we have buffering and explains how to switch it on and off.

这篇关于为什么我的perl守护程序无法打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆