advice for tailing a file

Daniel Carosone daniel.carosone at gmail.com
Tue Dec 15 09:25:22 CET 2015


Recommend trying to read more than one line from the pipe per wakeup,
possibly by wrapping in AnyEvent::Handle, which implements just the
buffering you need for this.

That will probably obviate the need for the throttle.
On 15 Dec 2015 16:29, "Scott Wiersdorf" <scott at ipartner.net> wrote:

> 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
>
> _______________________________________________
> anyevent mailing list
> anyevent at lists.schmorp.de
> http://lists.schmorp.de/mailman/listinfo/anyevent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20151215/35d4969d/attachment.html>


More information about the anyevent mailing list