how to use AnyEvent::Handle on the child of AnyEvent::Run (aka portable_socketpair)?

Fulko Hew fulko.hew at gmail.com
Tue Nov 19 21:03:27 CET 2013


I'm trying to use AnyEvent:Run, but I need to make my child smarter.
and make it an AnyEvent style process that uses 'event driven I/O'.
But I can't seem to get anywhere, and I'm obviously missing... something.

I'm attempting the test example below.

What I see is the child successfully sending and the parent receiving.
But I'm not getting the parent-to-child pat,  and I don't know if its a
problem in parent or in the child, or I'm just doing it wrong.

use strict;
use warnings;
use Socket;
use AnyEvent;
use AnyEvent::Run;
use AnyEvent::Handle;

sub send_msg_to_parent { print "$_[0]\n"; }

sub child_proc {
    my $child_hdl = new AnyEvent::Handle (
        fh  => \*STDIN,
        on_read => sub {
                       my $hdl = shift;
                       my $d = $hdl->{rbuf};
                       $hdl->{rbuf} = '';
                       send_msg_to_parent("child rx: $d");
                       },
    );
    my $child_timer = AnyEvent->timer (  # send a watchdog msg
        after       => 2,
        interval    => 2,
        cb          => sub {send_msg_to_parent("child timer"),
    );
    send_msg_to_parent("child started");
    my $child_cv = AnyEvent->condvar;
    $child_cv->wait;
}


my $child = AnyEvent::Run->new(
    cmd     => sub { child_proc() },
    on_read => sub {
                   my $hdl = shift;
                   my $d = $hdl->{rbuf};
                   $hdl->{rbuf} = '';
                   print "parent rx: $d";
                   },
    on_error    => sub { print "child died\n"; },
);
my $parent_timer = AnyEvent->timer (
    after       => 3,
    interval    => 3,
    cb          => sub { $child->push_write("sent to child\n"); print
"parent tx\n";},
);
$child->push_write("first send to child\n"); print "parent tx 'first
send'\n";
my $cv = AnyEvent->condvar;
$cv->wait;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20131119/da23b033/attachment.html>


More information about the anyevent mailing list