<p dir="ltr">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. </p>
<p dir="ltr">That will probably obviate the need for the throttle. </p>
<div class="gmail_quote">On 15 Dec 2015 16:29, "Scott Wiersdorf" <<a href="mailto:scott@ipartner.net">scott@ipartner.net</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, Dec 14, 2015 at 06:17:04PM -0500, Vikas N Kumar wrote:<br>
> If you're not planning to run on Windows, then you can just use the<br>
> "tail -F" command on the log file and pass that as a file handle to<br>
> open() and then that file handle can be read using AnyEvent. Then any<br>
> appends, or truncation or overwrites of the file can be handled by the<br>
> "tail" command and you just are reading its output.<br>
<br>
I took Vikas' advice and tried a little something with tail -F:<br>
<br>
open my $pipe, "-|", 'tail', '-F', $filename<br>
or die "Unable to open pipe to tail: $!\n";<br>
<br>
my $io = AnyEvent->io (fh => $pipe, poll => "r", cb => sub {<br>
my $line = <$pipe>;<br>
<br>
## do something with $line<br>
...<br>
<br>
EV::sleep 0.0005; ## throttle!<br>
});<br>
<br>
my $timer = AnyEvent->timer(<br>
after => 3,<br>
interval => 3,<br>
cb => sub {<br>
## do something periodically<br>
}<br>
);<br>
<br>
EV::run;<br>
<br>
It performs two orders of magnitude better than File::Tail and it's so<br>
simple! I don't know why I didn't consider this method first. Much<br>
thanks Vikas!<br>
<br>
I have a little throttle in the pipe reader (EV::sleep); is this a<br>
common way to slow things down a little? My "do something" probably<br>
needs some optimization, but sleeping here keeps the process under 10%<br>
CPU on my laptop (albeit with much lower throughput). Without the<br>
sleep it runs around 60%.<br>
<br>
Thanks all!<br>
<br>
Scott<br>
--<br>
Scott Wiersdorf<br>
<a href="mailto:scott@ipartner.net">scott@ipartner.net</a><br>
<br>
_______________________________________________<br>
anyevent mailing list<br>
<a href="mailto:anyevent@lists.schmorp.de">anyevent@lists.schmorp.de</a><br>
<a href="http://lists.schmorp.de/mailman/listinfo/anyevent" rel="noreferrer" target="_blank">http://lists.schmorp.de/mailman/listinfo/anyevent</a></blockquote></div>