AnyEvent::Fork clarification
Vikas N Kumar
vikas at vikaskumar.org
Mon Jan 6 23:41:36 CET 2014
Hi Marc
After browsing through your AnyEvent::Fork documentation multiple times
and also reading the source code, I can still not figure out how and why
the following example works:
<code>
AnyEvent::Fork->require('MyModule')->run('MyModule::myfunc', my $cv =
AE::cv);
my $stderr = $cv->recv;
</code>
The real trouble I have in understanding this example is what is the "my
$cv = AE::cv" line doing there ?
There is no <code>$cv->send </code> being called either.
I am trying to also do this in one of my examples:
<code>
open my $output, ">/tmp/aefork.log" or die "$!";
my $func = << 'EOF';
sub run_local {
my ($fh , $output, @cmd) = @_;
open STDOUT, ">&", $output or die "$!";
open STDERR, ">&", $output or die "$!";
exec @cmd;
}
EOF
my $pool = AnyEvent::Fork->new->eval($func)->send_fh($output);
my $cmd = "/bin/sleep";
my $cv;
for (1 .. 10) {
$pool->fork->send_arg($cmd, $_ + 10)->run("run_local", $cv = AE::cv);
}
my $stderr = $cv->recv;
</code>
The forking happens but I want the master/parent process to wait for all
the forks to finish before it exits.
However, it exits immediately after forking.
I would also like to know if I can track which process finishes first or
have something be called everytime a process finishes exec'ing. This is
why I wanted to use AnyEvent::Fork over Parallel::ForkManager.
Please help. I believe the AE::cv is the right way to go but I am using
it wrong somewhere and I cannot figure out why.
Thanks
Vikas
More information about the anyevent
mailing list