如何调用单个perl脚本通过循环并行运行不同的输入参数

DSD*_*DSD 2 parallel-processing perl

我是perl的新手.目前我正在运行一个perl脚本,它将调用另一个perl脚本.第二个perl脚本有2个输入参数

sample2.pl -itest.txt -ffile1.txt
Run Code Online (Sandbox Code Playgroud)

我有不同的输入参数-f一样file1,file2,file3...file10.

现在我想为当前运行的所有输入参数(file1,file2,file3)并行运行第二个perl脚本 -

#!/usr/bin/perl
use warnings;

use strict;

my $fi="output3.txt";--(output3.txt will contain the files file1,file2..file10)
    open (OF, $fi);

foreach(<OF>)
{
system ("perl ucm3.pl -iinput.txt -f$_ ");

print $_;
}
Run Code Online (Sandbox Code Playgroud)

但它并非并行运行,而是一个接一个地运行.请帮助平行运行这些脚本.提前致谢.

sim*_*que 6

您需要创建一个新进程并将其与主程序分离.你可以这样做徒步fork,但你也可以做到这一点与并行:: ForkManager.它会照顾你的一切.

use strict; use warnings;
use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new($MAX_PROCESSES);

open (my $fh, '<', "output3.txt") or die $!;
while (my $data = <$fh>) {
  chomp $data;

  # Forks and returns the pid for the child:
  my $pid = $pm->start and next;

  # we are now in the child process
  print system ("perl ucm3.pl -iinput.txt -f$data ");

  $pm->finish; # Terminates the child process
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果您还不熟悉Perl,请查看本手册.它将告诉您如何从CPAN获取Parallel :: FormManager(和其他东西).