AnyEvent and piped openfrom Sabrina

Marc Lehmann schmorp at schmorp.de
Fri Aug 19 05:14:12 CEST 2011


On Thu, Aug 18, 2011 at 11:30:19PM +0300, Sabrina Friedman <utebachmeier at gmail.com> wrote:
> 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.

It works also after process termination, but your program does something
else:

> 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; };

Here is the latest place where you can create the child watcher.

> $cv->recv;

Here it's too late - any number of events can happen here, specifically,
the child might have died already and cleaned up.

> $ev = AE::child $pid, sub { $cv->send("child"); };

too late.

> I would expect "Event received: child" in the 4th line.

For that, you are triggering it a bit too late - AnyEvent (or event loops)
cannot store the exit status indefinitely.

Compare this with io watchers: when a socket ("process") is fully connected, you can
create an I/O watcher on it. when the other side closes it ("exit"), you can still set
an I/O watcher, but when you close it (querying events), the socket is gone
and registering an I/O watcher is too late.

> There are modules out there to manage processes with AnyEvent, but

There's also AnyEvent::Util::run, which is quite a bit more powerful than
a piped open.

Somewhat tangential: are you sure you need a child watcher for a piped
open?

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list