perl Parallel :: ForkManager卡在这个脚本中?

时间:2014-12-30 10:44:49

标签: perl fork

我正在尝试并行运行Perl脚本并在此处陷入困境。请参阅示例脚本:

如果我在没有-fork 4选项的情况下运行它,它运行正常:

perl perl_parallel_forkmanager_ls.pl -limit 10                                                     
799c89a4c78eafbfb9e7962b8e9705f7  /etc/apt/trusted.gpg
ff163e8e9e38670705a9f2cea8b530c9  /etc/apt/trusted.gpg~
075e92fd5c6f0dcdad857603f03dd3a5  /etc/bash_completion.d/R
b269c1383a87a7da2cc309c929ba35ca  /etc/bash_completion.d/grub
7cbefff45508d2ed69576bebc80e66bb  /etc/bash_completion.d/docker
facb1fdc0fcf7f6b150442d1a9036795  /etc/bash_completion.d/pulseaudio-bash-completion.sh
69dfca7a7b55181cef06b9ed28debb20  /etc/gnome/defaults.list
a65e81e55558941ce0f3080b9333e18f  /etc/sensors3.conf
9e87bc86a77261acfb2bae618073a787  /etc/grub.d/20_linux_xen
8039709ee9648dabda0cdca713f2ed49  /etc/grub.d/30_os-prober
1bc18861cc2438517ce6b6c22fd4fa49  /etc/grub.d/10_linux

但是,如果我使用小于-fork 4的值-limit 10运行它,它会忽略limit的值:

perl perl_parallel_forkmanager_ls.pl -fork 4 -limit 10 2>/dev/null | wc -l
80

有什么想法吗?

#!/usr/bin/perl
use strict;
use warnings;
use Parallel::ForkManager;
use Getopt::Long;
my $dir = '/etc'; my $fork = 1; my $size = '9876'; my $limit;
my $verbose;
GetOptions(
       'dir:s' => \$dir,
           'fork:s' => \$fork,
           'size:s' => \$size,
           'limit:s' => \$limit,
       'verbose' => \$verbose,
          );
my $cmd; my $ret;
$cmd = "find $dir -size +".$size."c -type f 2>/dev/null";
open(P, "-|", "$cmd") or die "$cmd -- $!";
my $pm; $pm=new Parallel::ForkManager($fork) if ($fork > 1);
my $count = 0;
while (<P>) {
  if ($fork > 1) {
    $pm->start and next;
  }
  my $file = $_; chomp $file;
  my $md5 = `md5sum $file`;
  print "$md5";
  $pm->finish if ($fork > 1);
  $count++;
  last if (defined $limit && $count > $limit);
};
$pm->wait_all_children if ($fork > 1);
close P;

1 个答案:

答案 0 :(得分:3)

在给出$pm->finish时永远不会达到-fork > 1之后的语句。您应该更改while循环中语句的顺序:

while (<P>) {
    $count++; 
    last if (defined $limit && $count > $limit);
    if ($fork > 1) {
        $pm->start and next;
    }
    my $file = $_; chomp $file;
    my $md5 = `md5sum $file`;
    print "$md5";
    $pm->finish if ($fork > 1);
};