On Jun 16, 7:34 pm, knowsuperunkn...@gmail.com (Unknown User) wrote:
> Hi
>
> I wrote the following script to fork ssh processes with perl and
> Parallel::ForkManager:
>
> #!/usr/local/bin/perl -w
> use strict;
>
> my @cmd = "@ARGV";
> use Parallel::ForkManager;
> my @nodes;
> open(NODE,"<list") || die "!$\n";
> while(<NODE>) {
>         chomp;
>         push(@nodes,$_);
>
> }
>
> my   $pm = new Parallel::ForkManager(10);       # Max 10 procs
> for my $node(@nodes) {
>         my $pid = $pm->start and next;
>         my $res = `ssh $node "@cmd"`;
>         print "$res";
>         $pm->finish;
>
> }
>
> This works pretty well, but Since There is More than One Way to DO it,
> i wonder if anyone has another version that does not use fork?

IPC::Run is a possibility (although there's a fork under the
covers just as there is for the backtick command you're
using).

>From the IPC::Run doc:

   # Multiple children simultaneously (run() blocks until all
   # children exit, use start() for background execution):
      run \...@foo1, '&', \...@foo2;

So, untested:

use IPC::Run qw/run/;

my @tasks;
for my $node(@nodes) {
    my $task = sub {
                       my $res = qw{ ssh  $node "@cmd" };
                       print join "\n", "$node:",$res,"status:$?\n";
                     };
    push( @tasks, $task, '&' );
}

run @tasks  or die "run error: $?";

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to