File Coverage

examples/callback.pl
Criterion Covered Total %
statement 30 30 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 38 38 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2 7     7   37114 use lib '.';
  7         4732  
  7         35  
3 7     7   1120 use strict;
  7         14  
  7         161  
4 7     7   4676 use Parallel::ForkManager;
  7         42  
  7         551740  
5              
6 7         585690 my $max_procs = 3;
7 7         49 my @names = qw( Fred Jim Lily Steve Jessica Bob );
8             # hash to resolve PID's back to child specific information
9              
10 7         91 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     12   88 sub { my ($pid, $exit_code, $ident) = @_;
16 12         360 print "** $ident just got out of the pool ".
17             "with PID $pid and exit code: $exit_code\n";
18             }
19 7         77 );
20              
21             $pm->run_on_start(
22 21     21   291 sub { my ($pid,$ident)=@_;
23 21         762 print "** $ident started, pid: $pid\n";
24             }
25 7         56 );
26              
27             $pm->run_on_wait(
28             sub {
29 60     60   1041 print "** Have to wait for one children ...\n"
30             },
31 7         49 0.5,
32             );
33              
34 7         21 foreach my $child ( 0 .. $#names ) {
35 27 100       507 my $pid = $pm->start($names[$child]) and next;
36              
37             # This code is the child process
38 6         154 print "This is $names[$child], Child number $child\n";
39 6         30001596 sleep ( 2 * $child );
40 6         102 print "$names[$child], Child $child is about to get out...\n";
41 6         6001904 sleep 1;
42 6         343 $pm->finish($child); # pass an exit code to finish
43             }
44              
45 1         30 print "Waiting for Children...\n";
46 1         35 $pm->wait_all_children;
47 1         23 print "Everybody is out of the pool!\n";
48