简体   繁体   中英

Perl parallel programming

I'm not very expert with Perl, so I preferred to ask you. I have to launch two precess contemporary and wait them and I've done like this:

use Parallel::ForkManager;

for ($i=0; $i<2; $i++)
{
  $pm->start and next;
  @args = ("python", "myprogram");
  push(@args,split(/ +/, $param)); 
  system(@args) == 0 or die "system @args failed: $?";
  $pm->finish;
}

$pm->wait_all_children;

Is that correct or there is a better way to do it? Is it correct to use system with parallel?

The intended use of P::FM is to limit the number of children, but it will indeed work well for you here. A few nitpicks:

  1. There's a slight bug in your error reporting. Specifically, it could give you useless information. Change

     die "system @args failed: $?"; 

    to

     die "system @args failed: ".($? == -1 ? $! : $?)."\\n"; 
  2. Needless use of overly-complex C-style for loop. I'd change

     for ($i=0; $i<2; $i++) 

    to

     for my $i (0..1) 
  3. It looks like you're trying to parse a command line to avoid calling a shell, when you could simply call the shell.

     @args = ("python", "myprogram"); push(@args,split(/ +/, $param)); system(@args) 

    could be written as

     system("python myprogram $param") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM