I am looking at the AnyEvent::Fork
module. I have 20 external scripts I would like to invoke in parallel (6 at a time) and summarize their output later when all finished. I am at loss how to achieve this.
The example code (that only invokes 1 child) in the module has a problem. I just add a simple sleep to the code to not return at once and the parent exits immediately without waiting for the child process.
open my $output, ">/tmp/log" or die "$!";
AnyEvent::Fork
->new
->eval ('
# compile a helper function for later use
sub run {
my ($fh, $output, @cmd) = @_;
# perl will clear close-on-exec on STDOUT/STDERR
open STDOUT, ">&", $output or die;
open STDERR, ">&", $fh or die;
### Added by me to demonstrate that
### $cv->recv returns immediately.
sleep 5;
exec @cmd;
}
')
->send_fh ($output)
->send_arg ("/bin/echo", "hi")
->run ("run", my $cv = AE::cv);
my $stderr = $cv->recv;
The result is that /tmp/log
is empty.
I do not get how condvar
is used here, it is not in the documentation. Can I get the number of running children using condvar
?
Please help how to get this right.
UPDATE the main issue here is that the parent does not wait for the child to complete.