开发者

While cycle and usleep

开发者 https://www.devze.com 2023-02-27 01:15 出处:网络
Here is a cut from my daemon code: package somepackage; use Proc::Daemon; use Time::HiRes qw/usleep/; use constant{

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消