Skip to content

Instantly share code, notes, and snippets.

@qgp9
Created November 10, 2016 07:58
Show Gist options
  • Save qgp9/9933da50f46ca4806ab5a63dd6a3ab38 to your computer and use it in GitHub Desktop.
Save qgp9/9933da50f46ca4806ab5a63dd6a3ab38 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.18;
use Time::HiRes 'time';
use Parallel::ForkManager;
my $pm = new Parallel::ForkManager(4);
my @links = 1..60;
my $btime = time;
foreach my $link (@links) {
$pm->start and next; # do the fork
say ((time-$btime),"\t",$link);
$pm->finish; # do the exit in the child process
}
$pm->wait_all_children;
#!/usr/bin/env perl
use v5.18;
use Time::HiRes 'time';
my @links = 1..60;
my $btime = time;
my $nfork = 0;
foreach my $link (@links) {
my $pid = fork();
die if not defined $pid;
if ( $pid ){
$nfork++;
if ($nfork > 4){
wait;
$nfork--;
}
}else{
sleep 2;
say ((time-$btime),"\t",$link);
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment