| 1 | #!/usr/bin/perl -w | ||||||||||
| 2 | 7 | ▶ | use lib '.'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use Parallel::ForkManager; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | my $max_procs = 3; | |||||||||
| 7 | 7 | my @names = qw( Fred Jim Lily Steve Jessica Bob ); | |||||||||
| 8 | # hash to resolve PID's back to child specific information | ||||||||||
| 9 | |||||||||||
| 10 | 7 | my $pm = Parallel::ForkManager->new($max_procs); | |||||||||
| 11 | |||||||||||
| 12 | # Setup a callback for when a child finishes up so we can | ||||||||||
| 13 | # get it's exit code | ||||||||||
| 14 | $pm->run_on_finish( | ||||||||||
| 15 | 12 | ▶ | sub { my ($pid, $exit_code, $ident) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 12 | print "** $ident just got out of the pool ". | |||||||||
| 17 | "with PID $pid and exit code: $exit_code\n"; | ||||||||||
| 18 | } | ||||||||||
| 19 | 7 | ); | |||||||||
| 20 | |||||||||||
| 21 | $pm->run_on_start( | ||||||||||
| 22 | 21 | ▶ | sub { my ($pid,$ident)=@_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 21 | print "** $ident started, pid: $pid\n"; | |||||||||
| 24 | } | ||||||||||
| 25 | 7 | ); | |||||||||
| 26 | |||||||||||
| 27 | $pm->run_on_wait( | ||||||||||
| 28 | sub { | ||||||||||
| 29 | 57 | ▶ | print "** Have to wait for one children ...\n" | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 30 | }, | ||||||||||
| 31 | 7 | 0.5, | |||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | 7 | foreach my $child ( 0 .. $#names ) { | |||||||||
| 35 | 27 | ▶ | my $pid = $pm->start($names[$child]) and next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | # This code is the child process | ||||||||||
| 38 | 6 | print "This is $names[$child], Child number $child\n"; | |||||||||
| 39 | 6 | sleep ( 2 * $child ); | |||||||||
| 40 | 6 | print "$names[$child], Child $child is about to get out...\n"; | |||||||||
| 41 | 6 | sleep 1; | |||||||||
| 42 | 6 | $pm->finish($child); # pass an exit code to finish | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 1 | print "Waiting for Children...\n"; | |||||||||
| 46 | 1 | $pm->wait_all_children; | |||||||||
| 47 | 1 | print "Everybody is out of the pool!\n"; | |||||||||
| 48 | |||||||||||