AnyEvent and piped open

Laurent laurent.aml at gmail.com
Thu Jul 14 18:09:03 CEST 2011


Hi,

I am using AnyEvent/EV and try to catch the process termination of a piped open.
This works when the child event is set before process termination, and
fails if it is set after.
In the later case, close still fails, which seems to indicate that
AnyEvent/EV somehow catched the process termination.
I tested this on OSX.

Here is the testcase:


use common::sense;
use EV;
use AnyEvent;

sub tprint { print AE::time, " ", @_; }

AnyEvent::detect();
$|=1;
my $fd;
my $pid = open($fd, '-|', '/bin/echo', 'hello');
tprint "Wait for the process $pid to complete.\n";
my $ev; $ev = AE::io $fd, 0, sub { my $b; sysread($fd, $b, 16); tprint
"-- $b"; undef $ev; };
my $cv = AE::cv;
my $to = AE::timer 1, 0, sub { $cv->send; };
$cv->recv;
tprint "Now wait for the child sig.\n";
$cv = AE::cv;
$ev = AE::child $pid, sub { $cv->send("child"); };
$to = AE::timer 1, 0, sub { $cv->send("timer"); };
my $r = $cv->recv;
tprint "Event received: $r.\n";
$r = close($fd);
tprint "As the child has already been catched, close returns an error.
This is expected.\n";
tprint "Close returned ".($r ? "ok" : "error").": exitcode=$? error=$!\n";

And its output:

1310658749.79279 Wait for the process 2684 to complete.
1310658749.79377 -- hello
1310658750.78514 Now wait for the child sig.
1310658751.78507 Event received: timer.
1310658751.78561 As the child has already been catched, close returns
an error. This is expected.
1310658751.78578 Close returned error: exitcode=-1 error=No child processes


I would expect "Event received: child" in the 4th line.
There are modules out there to manage processes with AnyEvent, but
they seem a bit overkill vs piped open.

Thanks,

-- Laurent



More information about the anyevent mailing list