While cycle and usleep
Here is a cut from my daemon code:
package somepackage;
use Proc::Daemon;
use Time::HiRes qw/usleep/;
use constant{
LOGFILE => '/var/log/.../work.log'
}开发者_开发百科;
our $LOG;
unless($pid)
{
open($LOG,'>>'.LOGFILE);
my $tm;
}
while ($pid == 0)
{
$tm=usleep(999940); #to be more accurate, 1 sec
print $somepackage::LOG $tm."\n";
}
but the problem is, that sometimes print writes to file only after let's say 10 seconds. I know why it happens, because cycle doesn't wait while print writes to file, instead it goes for a new loop and then sleeps. How can I wait, while print finishes writing to a file and then go to the next iteration?
I think you're talking about flush.
Call $LOG->autoflush(1)
once, right after you open your log.
(You might need to add use IO::Handle
too).
精彩评论