File Coverage

examples/callback_data.pl
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod n/a
total 32 32 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 6     6   32010 use strict;
  6         9  
  6         246  
3 6     6   33 use warnings;
  6         9  
  6         357  
4              
5 6     6   4245 use Parallel::ForkManager;
  6         36  
  6         656646  
6              
7 6         837555 my $max_procs = 2;
8 6         45 my @names = qw( Fred Jim );
9              
10 6         96 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             $pm->run_on_finish( sub {
15 4     4   15 my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data_structure_reference) = @_;
16 4         53 print "$ident just got out of the pool ".
17             "with exit code: $exit_code and data: @$data_structure_reference\n";
18 6         72 });
19              
20             $pm->run_on_start( sub {
21 6     6   133 my ($pid,$ident)=@_;
22 6         273 print "$ident started\n";
23 6         84 });
24              
25 6         33 foreach my $child ( 0 .. $#names ) {
26 10 100       176 my $pid = $pm->start($names[$child]) and next;
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         74 my @result = ($names[$child], length $names[$child]);
32 4         8001104 sleep 1+rand(3);
33              
34             # pass an exit code and data stucture to finish
35 4         212 $pm->finish($child, \@result );
36             }
37              
38             #print "Waiting for Children...\n";
39 2         167 $pm->wait_all_children;
40 2         65 print "Everybody is out of the pool!\n";
41              
42