| 1 | #!/usr/bin/perl | ||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | ▶ | use Parallel::ForkManager; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 6 | my $max_procs = 2; | |||||||||
| 8 | 6 | my @names = qw( Fred Jim ); | |||||||||
| 9 | |||||||||||
| 10 | 6 | my $pm = Parallel::ForkManager->new($max_procs, @ARGV); | |||||||||
| 11 | |||||||||||
| 12 | # Setup a callback for when a child finishes up so we can | ||||||||||
| 13 | # get it's exit code and any data it collected | ||||||||||
| 14 | 4 | ▶ | $pm->run_on_finish( sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 4 | my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_; | |||||||||
| 16 | 4 | print "$ident just got out of the pool ". | |||||||||
| 17 | "with exit code: $exit_code and data: @$data_structure_reference\n"; | ||||||||||
| 18 | 6 | }); | |||||||||
| 19 | |||||||||||
| 20 | 6 | ▶ | $pm->run_on_start( sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 6 | my ($pid,$ident)=@_; | |||||||||
| 22 | 6 | print "$ident started\n"; | |||||||||
| 23 | 6 | }); | |||||||||
| 24 | |||||||||||
| 25 | 6 | foreach my $child ( 0 .. $#names ) { | |||||||||
| 26 | 10 | ▶ | my $pid = $pm->start($names[$child]) and next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | # This code is the child process | ||||||||||
| 29 | # We can do here anything and obtain any data. | ||||||||||
| 30 | # The result can be any array or hash. | ||||||||||
| 31 | 4 | my @result = ($names[$child], length $names[$child]); | |||||||||
| 32 | 4 | sleep 1+rand(3); | |||||||||
| 33 | |||||||||||
| 34 | # pass an exit code and data stucture to finish | ||||||||||
| 35 | 4 | $pm->finish($child, \@result ); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | #print "Waiting for Children...\n"; | ||||||||||
| 39 | 2 | $pm->wait_all_children; | |||||||||
| 40 | 2 | print "Everybody is out of the pool!\n"; | |||||||||
| 41 | |||||||||||
| 42 | |||||||||||