AnyEvent::Fork clarification

Vikas N Kumar vikas at vikaskumar.org
Tue Jan 7 03:08:12 CET 2014


Nevermind, I figured it out and this works.

Here is what I am doing, and maybe you can suggest a better way.


<code>
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 = AE::cv;
my @watchers = ();
my $count = 0;
for (1 .. 10) {
    $pool->fork->send_arg($cmd, $_ + 10)->run("run_local", sub {
		my $fh  = shift;
		push @watchers, AnyEvent->io(fh => $fh, poll => "w", cb => sub {
			close $fh;
			$count++;
			$cv->send if $count >= 10;
		});
	});
 }
$cv->recv;
</code>




More information about the anyevent mailing list