Anything wrong with using AnyEvent::Handle for files?

Daniel Carosone daniel.carosone at gmail.com
Sat Aug 8 00:56:10 CEST 2015


Summary:

   - regular files are always ready to read in select/poll etc, even if
   actually reading from them would be a blocking operation in the VFS layer.
   - for small sequential io to local files, with kernel buffering and
   pre-fetch, and some luck, you might not notice so much; most reads will be
   satisfied more or less immediately, and writes will be deferred.
   - for other circumstances (seeks, cache misses, nfs, disk contention,
   disk errors, memory pressure, etc) you might block for an indeterminate
   amount of time

The point of the recommendation is not that it won't work, it's that it
won't work like you might expect for what should be an event-driven
non-blocking program.  You might also starve/delay other IO
watchers/handles, and thrash cpu cycles, just because the file is always
ready.

Depending on your IO pattern, a common workaround is to fork a process to
read or write the real file (blocking as needed) and communicate with it
via a pipe. Or use the AnyEvent::IO / IOAIO modules instead (which does
something similar to protect against blocking via pthreads), with a
different interface. Or, assess the risk and your requirements carefully,
and decide you're happy to block on file IO occasionally.

I would like to see an aio_* based Handle backend that made simple
streaming file sequential IO "Just Work" for non-blocking AE programs.
Handle already deals with the buffering and partial-read handling needed
when using the aio_ interfaces.

On 8 August 2015 at 01:19, Mike Schilli <office at perlmeister.com> wrote:

> Although the documentation seems to discourage using AnyEvent::Handle
> for anything but streaming things like sockets and pipes, using it for
> files seems to work fine in the code below.
> Any reason not to use it in this way?
>
> use AnyEvent;
> use AnyEvent::Handle;
>
> open my $fh, "<", "foo.txt" or die;
>
> my $handle = AnyEvent::Handle->new(
>     fh       => $fh,
>     on_error => sub { die "read error: $!"; },
>     on_eof   => sub { print "done!\n"; },
>     on_read  => sub {
>         my( $hdl ) = @_;
>
>         $hdl->push_read( line => sub {
>             my( $h, $line ) = @_;
>             print "line=$line\n";
>         } );
>     },
> );
>
> AnyEvent->condvar->recv;
>
> _______________________________________________
> anyevent mailing list
> anyevent at lists.schmorp.de
> http://lists.schmorp.de/mailman/listinfo/anyevent




-- 
Dan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20150808/e5455b69/attachment-0001.html>


More information about the anyevent mailing list