如何调用单个perl脚本以针对不同的输入参数通过循环并行运行 [英] how to call single perl script to run parallely through loop for different input parameters

查看:243
本文介绍了如何调用单个perl脚本以针对不同的输入参数通过循环并行运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new in perl. currently i am running a perl scripts which will call another perl script. the second perl script is having 2 input parameters

sample2.pl -itest.txt -ffile1.txt

对于-f,我有不同的输入参数,例如file1,file2,file3...file10.

I have different input parameters for -f like file1,file2,file3...file10.

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

Now i want to run the second perl script parallel for all the input parameters(file1,file2,file3) currently i m running as -

#!/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 $_;
}

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

But it is not runnng parallely it is running one after the other. PLease help to run these script parallely. Thanks in advance.

推荐答案

您需要创建一个新进程并将其与主程序分离.您可以通过使用fork 进行此操作,但是也可以使用 Parallel :: ForkManager .它将为您处理一切.

You need to create a new process and detach it from your main program. You could do this on foot with fork, but you could also do this with Parallel::ForkManager. It will take care of everything for you.

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
}

编辑:如果您还不熟悉Perl,请查看

Edit: If you are not yet familiar with Perl, take a look at this manual. It will tell you how to get Parallel::FormManager (and other things) from CPAN.

这篇关于如何调用单个perl脚本以针对不同的输入参数通过循环并行运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆