简体   繁体   中英

perl Parallel::ForkManager stuck in this script?

I am trying to run a Perl script in parallel and got stuck at a point here. See the example script:

If I run it without the -fork 4 option, it runs fine:

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

But if I run it with a value of -fork 4 smaller than the value of -limit 10 , it ignores the value of limit:

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

Any ideas?

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

The statements after $pm->finish are never reached when -fork > 1 is given.. You should change the order of the statements in the while loop:

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);
};

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