advice for tailing a file
Scott Wiersdorf
scott at ipartner.net
Tue Dec 15 06:27:42 CET 2015
On Mon, Dec 14, 2015 at 06:17:04PM -0500, Vikas N Kumar wrote:
> If you're not planning to run on Windows, then you can just use the
> "tail -F" command on the log file and pass that as a file handle to
> open() and then that file handle can be read using AnyEvent. Then any
> appends, or truncation or overwrites of the file can be handled by the
> "tail" command and you just are reading its output.
I took Vikas' advice and tried a little something with tail -F:
open my $pipe, "-|", 'tail', '-F', $filename
or die "Unable to open pipe to tail: $!\n";
my $io = AnyEvent->io (fh => $pipe, poll => "r", cb => sub {
my $line = <$pipe>;
## do something with $line
...
EV::sleep 0.0005; ## throttle!
});
my $timer = AnyEvent->timer(
after => 3,
interval => 3,
cb => sub {
## do something periodically
}
);
EV::run;
It performs two orders of magnitude better than File::Tail and it's so
simple! I don't know why I didn't consider this method first. Much
thanks Vikas!
I have a little throttle in the pipe reader (EV::sleep); is this a
common way to slow things down a little? My "do something" probably
needs some optimization, but sleeping here keeps the process under 10%
CPU on my laptop (albeit with much lower throughput). Without the
sleep it runs around 60%.
Thanks all!
Scott
--
Scott Wiersdorf
scott at ipartner.net
More information about the anyevent
mailing list