Forker.pm

filter: Statementstmt 44.5% 82 / 184 Branchbran 32.8% 23 / 70 Conditioncond 19.1% 9 / 47 MC/DCmcdc 15.3% 4 / 26 Subroutinesub 68.9% 20 / 29 Podpod 86.9% 20 / 23 totaltotal 41.6% 158 / 379 SCAR 69.8
CC
65
Cov
38%
write_tree
812.0
min
30.0
max
30.0
CRAP 1078.1
↑ Summary Forker/Process.pm »
1 # See copyright, etc in below POD section.
2 ######################################################################
3  
4 package Parallel::Forker;
5 34 require 5.006;
6 34 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 34 ▶use Parallel::Forker::Process;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use vars qw($Debug $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 34 $VERSION = '1.254';
15  
16 ######################################################################
17 #### CONSTRUCTOR
18  
19 sub new {
20 53 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.4
Pod documented
21 53 my $self = {
22 _activity => 1, # Optionally set true when a sig_child comes
23 _processes => {}, # All process objects, keyed by id
24 _labels => {}, # List of process objects, keyed by label
25 _runable => {}, # Process objects runable now, keyed by id
26 _running => {}, # Process objects running now, keyed *PID*
27 _run_after_eqn => undef,# Equation to eval to determine if ready to launch
28 _parent_pid => $$, # PID of initial process creating the forker
29 max_proc => undef, # Number processes to launch, <1=any, +=that number
30 use_sig_child => undef, # Default to not using SIGCHLD handler
31 @_
32 };
33 53 ▶ bless $self, ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0530
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
34 53 return $self;
35 }
36  
37 #### ACCESSORS
38  
39 sub in_parent {
40 1 ▶ my $self = shift;
Subroutine in_parent: called CC=1 SCAR=0.0
Pod documented
41 1 return $self->{_parent_pid}==$$;
42 }
43  
44 sub max_proc {
45 9 ▶ my $self = shift;
Subroutine max_proc: called CC=2 SCAR=7.1
Pod documented
46 9 ▶ $self->{max_proc} = shift if $#_>=0;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 9 0 9
47 9 return $self->{max_proc};
48 }
49  
50 sub use_sig_child {
51 939 ▶ my $self = shift;
Subroutine use_sig_child: called CC=2 SCAR=6.9
Pod documented
52 939 ▶ $self->{use_sig_child} = shift if $#_>=0;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ >= 0 1 938 939
53 939 return $self->{use_sig_child};
54 }
55  
56 sub running {
57 23 ▶ my $self = shift;
Subroutine running: called CC=1 SCAR=0.0
Pod documented
58 23 return (values %{$self->{_running}});
59 }
60  
61 sub running_sorted {
62 0 ▶ my $self = shift;
Subroutine running_sorted: not called CC=1 SCAR=6.9
Pod undocumented
63 0 return (sort {$a->{name} cmp $b->{name}} values %{$self->{_running}});
64 }
65  
66 sub process {
67 0 ▶ my $self = shift;
Subroutine process: not called CC=2 SCAR=17.9
Pod documented
68 0 ▶ confess "usage: \$fork->process(\$name)" unless scalar(@_) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 1 0 0 0
69 0 return $self->{_processes}{$_[0]};
70 }
71  
72 sub processes {
73 52 ▶ my $self = shift;
Subroutine processes: called CC=1 SCAR=0.0
Pod documented
74 52 return (values %{$self->{_processes}});
75 }
76  
77 sub processes_sorted {
78 42 ▶ my $self = shift;
Subroutine processes_sorted: called CC=1 SCAR=0.0
Pod documented
79 42 return (sort {$a->{name} cmp $b->{name}} values %{$self->{_processes}});
80 }
81  
82 sub state_stats {
83 0 ▶ my $self = shift;
Subroutine state_stats: not called CC=1 SCAR=6.9
Pod documented
84 0 my %stats = (idle=>0, ready=>0, running=>0, runable=>0,
85 done=>0, parerr=>0, reapable=>0);
86 0 map {$stats{$_->state}++} $self->processes;
87 0 return %stats;
88 }
89  
90 #### METHODS
91  
92 sub schedule {
93 414 ▶ my $class = shift;
Subroutine schedule: called CC=1 SCAR=0.0
Pod documented
94 414 return Parallel::Forker::Process->_new(_forkref=>$class,
95 @_);
96 }
97  
98 sub sig_child {
99 # Keep minimal to avoid coredumps
100 167 ▶ return if !$_[0];
Subroutine sig_child: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 167 167
101 167 $_[0]->{_activity} = 1;
102 }
103  
104 sub wait_all {
105 52 ▶ my $self = shift;
Subroutine wait_all: called CC=2 SCAR=6.9
Pod documented
106 52 ▶ while ($self->is_any_left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_any_left) 709 24 733
107 #print "NRUNNING ", scalar ( (keys %{$self->{_running}}) ), "\n";
108 709 $self->poll;
109 681 usleep 100*1000;
110 };
111 }
112  
113 sub reap_processes {
114 0 ▶ my $self = shift;
Subroutine reap_processes: not called CC=3 SCAR=24.8
Pod documented
115  
116 0 my @reaped;
117 0 foreach my $process ($self->processes) {
118 0 ▶ next unless $process->is_reapable;
Branch: 1 branch
BranchTrueFalseTotal
unless $process->is_reapable 0 0 0
119 0 $process->reap;
120 0 push @reaped, $process;
121 }
122 0 return @reaped;
123 }
124  
125 sub is_any_left {
126 733 ▶ my $self = shift;
Subroutine is_any_left: called CC=3 SCAR=11.0
Pod documented
127 733 ▶ return 1 if ( (keys %{$self->{_runable}}) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"_runable"};} > 0 141 592 733
128 592 ▶ return 1 if ( (keys %{$self->{_running}}) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"_running"};} > 0 568 24 592
129 }
130  
131 sub find_proc_name {
132 426 ▶ my $self = shift;
Subroutine find_proc_name: called CC=3 SCAR=11.0
Pod documented
133 426 my $name = shift;
134 # Returns list of processes matching the name or label
135 426 ▶ if (exists $self->{_processes}{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'_processes'}{$name}) { } 330 96 426
elsif (exists $self->{'_labels'}{$name}) { } 64 32 96
136 330 return ($self->{_processes}{$name});
137 } elsif (exists $self->{_labels}{$name}) {
138 64 return @{$self->{_labels}{$name}};
139 }
140 32 return undef;
141 }
142  
143 34 our $_Warned_Use_Sig_Child;
144  
145 sub poll {
146 709 ▶ my $self = shift;
Subroutine poll: called CC=12 SCAR=26.8
Pod documented
147 709 ▶ return if $self->use_sig_child && !$self->{_activity};
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_sig_child and not $self->{"_activity"} 480 229 709
Condition100%3/3
$self->use_sig_child and not $self->{"_activity"}
!ll&&!rl&&r
14215480
MC/DC100%2/2
$self->use_sig_child and not $self->{"_activity"}
$self->use_sig_child not $self->{"_activity"}
Truth table100%3/3
A and not(B)
A= $self->use_sig_childB= not $self->{"_activity"}
ABresult
0X0
100
111
148 229 ▶ if (!defined $self->use_sig_child) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->use_sig_child) 0 229 229
149 0 ▶ carp "%Warning: Forker object should be new'ed with use_sig_child=>0 or 1, "
Branch: 1 branch
BranchTrueFalseTotal
if $^W and not $Parallel::Forker::_Warned_Use_Sig_Child 0 0 0
Condition0%0/3
$^W and not $Parallel::Forker::_Warned_Use_Sig_Child
!ll&&!rl&&r
000
MC/DC0%0/2
$^W and not $Parallel::Forker::_Warned_Use_Sig_Child
$^W not $Parallel::Forker::_Warned_Use_Sig_Child
Truth table0%0/3
A and not(B)
A= $^WB= not $Parallel::Forker::_Warned_Use_Sig_Child
ABresult
0X0
100
111
150 if ($^W && !$_Warned_Use_Sig_Child);
151 0 $_Warned_Use_Sig_Child = 1;
152 0 $self->use_sig_child(0);
153 }
154  
155 # We don't have a loop around this any more, as we want to allow
156 # applications to do other work. We'd also need to be careful not to
157 # set _activity with no one runnable, as it would potentially cause a
158 # infinite loop.
159  
160 229 $self->{_activity} = 0;
161 229 my $nrunning = grep { not $_->poll } (values %{$self->{_running}});
162  
163 229 ▶ if (!($self->{max_proc} && $nrunning >= $self->{max_proc})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"max_proc"} and $nrunning >= $self->{"max_proc"}) 229 0 229
Condition66%2/3
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
!ll&&!rl&&r
197320
MC/DC0%0/2
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
$self->{"max_proc"} $nrunning >= $self->{"max_proc"}
Truth table66%2/3
A and B
A= $self->{"max_proc"}B= $nrunning >= $self->{"max_proc"}
ABresult
0X0
100
111
164 229 foreach my $procref (sort {$a->{name} cmp $b->{name}} # Lanch in named order
165 229 values %{$self->{_runable}}) {
166 243 ▶ last if ($self->{max_proc} && $nrunning >= $self->{max_proc});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_proc"} and $nrunning >= $self->{"max_proc"} 20 223 243
Condition100%3/3
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
!ll&&!rl&&r
1794420
MC/DC100%2/2
$self->{"max_proc"} and $nrunning >= $self->{"max_proc"}
$self->{"max_proc"} $nrunning >= $self->{"max_proc"}
Truth table100%3/3
A and B
A= $self->{"max_proc"}B= $nrunning >= $self->{"max_proc"}
ABresult
0X0
100
111
167 223 $procref->run;
168 195 $nrunning++;
169 }
170 }
171 # If no one's running, we need _activity set to check for runable -> running
172 # transitions during the next call to poll().
173 201 ▶ $self->{_activity} = 1 if !$nrunning;
Branch: 1 branch
BranchTrueFalseTotal
if not $nrunning 24 177 201
174 }
175  
176 sub ready_all {
177 52 ▶ my $self = shift;
Subroutine ready_all: called CC=3 SCAR=11.2
Pod documented
178 52 foreach my $procref ($self->processes) {
179 414 ▶ $procref->ready if $procref->is_idle;
Branch: 1 branch
BranchTrueFalseTotal
if $procref->is_idle 414 0 414
180 };
181 }
182  
183 sub kill_all {
184 0 ▶ my $self = shift;
Subroutine kill_all: not called CC=3 SCAR=24.8
Pod documented
185 0 ▶ my $signal = shift || 9;
Condition0%0/2
shift() || 9
l!l
00
MC/DC0%0/1
shift() || 9
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
186 0 foreach my $procref ($self->running_sorted) {
187 0 $procref->kill($signal);
188 };
189 }
190  
191 sub kill_tree_all {
192 0 ▶ my $self = shift;
Subroutine kill_tree_all: not called CC=3 SCAR=24.8
Pod documented
193 0 ▶ my $signal = shift || 9;
Condition0%0/2
shift() || 9
l!l
00
MC/DC0%0/1
shift() || 9
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
194 0 foreach my $procref ($self->running_sorted) {
195 0 $procref->kill_tree($signal);
196 };
197 }
198  
199 sub write_tree {
200 0 ▶ my $self = shift;
Subroutine write_tree: not called CC=28 SCAR=67.0
Pod documented
201 0 my %params = (@_);
202 0 ▶ defined $params{filename} or croak "%Error: filename not specified,";
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"filename"} or croak("%Error: filename not specified,") 0 0 0
203  
204 0 my %did_print;
205 0 my $another_loop = 1;
206 0 my $level = 0;
207 0 my $line = 4;
208 0 my @lines;
209 0 ▶ while ($another_loop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($another_loop) 0 0 0
210 0 $another_loop = 0;
211 0 $level++;
212 proc:
213 0 foreach my $procref ($self->processes_sorted) {
214 0 foreach my $ra (values %{$procref->{_after_parents}}) {
215 0 ▶ next proc if (($did_print{$ra->{name}}{level}||999) >= $level);
Branch: 1 branch
BranchTrueFalseTotal
if ($did_print{$ra->{"name"}}{"level"} || 999) >= $level 0 0 0
Condition0%0/2
$did_print{$ra->{'name'}}{'level'} || 999
l!l
00
MC/DC0%0/1
$did_print{$ra->{'name'}}{'level'} || 999
$did_print{$ra->{'name'}}{'level'}
Truth table0%0/2
A
A= $did_print{$ra->{'name'}}{'level'}
Aresult
11
00
216 }
217 0 ▶ if (!$did_print{$procref->{name}}{level}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($did_print{$procref->{"name"}}{"level"}) 0 0 0
218 0 $did_print{$procref->{name}}{level} = $level;
219 0 $did_print{$procref->{name}}{line} = $line;
220 0 $another_loop = 1;
221 0 $lines[$line][0] = $procref->_write_tree_line($level,0);
222 0 $lines[$line+1][0] = $procref->_write_tree_line($level,1);
223 0 foreach my $ra (values %{$procref->{_after_parents}}) {
224 $lines[$line][$did_print{$ra->{name}}{line}]
225 0 = $procref->{_after_parents_op}{$ra->{name}};
226 }
227 0 $line+=2;
228 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Parallel::Forker::Debug) 0 0 0
229 0 $lines[$line++][0] = $procref->_write_tree_line($level,2);
230 0 $lines[$line++][0] = $procref->_write_tree_line($level,3);
231 0 $lines[$line++][0] = $procref->_write_tree_line($level,4);
232 }
233 0 $line++;
234 }
235 }
236 }
237 0 $line++;
238  
239 0 if (0) {
240 for (my $row=1; $row<$line; $row++) {
241 for (my $col=1; $col<$line; $col++) {
242 print ($lines[$row][$col]?1:0);
243 }
244 print "\n";
245 }
246 }
247  
248 0 ▶ for (my $col=1; $col<=$#lines; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col <= $#lines) 0 0 0
249 0 my $col_used_row_min;
250 my $col_used_row_max;
251 0 ▶ for (my $row=1; $row<=$#lines; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= $#lines) 0 0 0
252 0 ▶ if ($lines[$row][$col]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[$row][$col]) 0 0 0
253 0 $col_used_row_min = min($col_used_row_min, $row);
254 0 $col_used_row_max = max($col_used_row_max, $row);
255 }
256 }
257 0 ▶ if ($col_used_row_min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_used_row_min) 0 0 0
258 0 $col_used_row_min = min($col_used_row_min, $col);
259 0 $col_used_row_max = max($col_used_row_max, $col);
260 0 ▶ for (my $row=$col_used_row_min; $row<=$col_used_row_max; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= $col_used_row_max) 0 0 0
261 0 ▶ $lines[$row][$col] ||= '<' if $row==$col;
Branch: 1 branch
BranchTrueFalseTotal
if $row == $col 0 0 0
Condition0%0/2
$lines[$row][$col] ||= "<"
l!l
00
MC/DC0%0/1
$lines[$row][$col] ||= "<"
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
Aresult
11
00
262 0 ▶ $lines[$row][$col] ||= '|';
Condition0%0/2
$lines[$row][$col] ||= "|"
l!l
00
MC/DC0%0/1
$lines[$row][$col] ||= "|"
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
Aresult
11
00
263 }
264 0 ▶ for (my $row=1; $row<=$#lines; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= $#lines) 0 0 0
265 0 ▶ if (($lines[$row][0]||" ") !~ /^ /) { # Line with text on it
Branch: 1 branch
BranchTrueFalseTotal
unless (($lines[$row][0] || " ") =~ /^ /) 0 0 0
Condition0%0/2
$lines[$row][0] || ' '
l!l
00
MC/DC0%0/1
$lines[$row][0] || ' '
$lines[$row][0]
Truth table0%0/2
A
A= $lines[$row][0]
Aresult
11
00
266 0 ▶ $lines[$row][$col] ||= '-';
Condition0%0/2
$lines[$row][$col] ||= "-"
l!l
00
MC/DC0%0/1
$lines[$row][$col] ||= "-"
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
Aresult
11
00
267 #$lines[$row][$col-1] ||= '-';
268 }
269  
270 0 ▶ $lines[$row][$col] ||= ' ';
Condition0%0/2
$lines[$row][$col] ||= " "
l!l
00
MC/DC0%0/1
$lines[$row][$col] ||= " "
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
Aresult
11
00
271 #$lines[$row][$col-1] ||= ' ';
272 }
273 }
274 }
275  
276 0 ▶ my $fh = IO::File->new($params{filename},"w") or die "%Error: $! $params{filename},";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($params{"filename"}, "w") or die qq[%Error: $! $params{"filename"},] 0 0 0
277 0 print $fh "Tree of process spawn requirements:\n";
278 0 print $fh " & Indicates the program it connects to must complete with ok status\n";
279 0 print $fh " before the command on this row is allowed to become RUNABLE\n";
280 0 print $fh " E As with &, but with error status\n";
281 0 print $fh " ^ As with &, but with error or ok status\n";
282 0 print $fh " O Ored condition, either completing starts proc\n";
283 0 print $fh "\n";
284 0 ▶ for (my $row=1; $row<=$#lines; $row++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row <= $#lines) 0 0 0
285 0 my $line = "";
286 0 ▶ for (my $col=1; $col<$#lines; $col++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col < $#lines) 0 0 0
287 0 ▶ $line .= ($lines[$row][$col]||"");
Condition0%0/2
$lines[$row][$col] || ''
l!l
00
MC/DC0%0/1
$lines[$row][$col] || ''
$lines[$row][$col]
Truth table0%0/2
A
A= $lines[$row][$col]
Aresult
11
00
288 }
289 0 ▶ $line .= $lines[$row][0]||"";
Condition0%0/2
$lines[$row][0] || ''
l!l
00
MC/DC0%0/1
$lines[$row][0] || ''
$lines[$row][0]
Truth table0%0/2
A
A= $lines[$row][0]
Aresult
11
00
290 0 $line =~ s/\s+$//;
291 0 print $fh "$line\n"; #if $line !~ /^\s*$/;
292 }
293  
294 0 $fh->close;
295 }
296  
297 sub min {
298 0 ▶ my $rtn = shift;
Subroutine min: not called CC=5 SCAR=34.0
Pod undocumented
299 0 foreach my $v (@_) {
300 0 ▶ $rtn = $v if !defined $rtn || (defined $v && $v < $rtn);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rtn or defined $v and $v < $rtn 0 0 0
Condition0%0/6
not defined $rtn or defined $v and $v < $rtn
l!l&&r!l&&!r
000
defined $v and $v < $rtn
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $rtn or defined $v and $v < $rtn
not defined $rtn defined $v $v < $rtn
Truth table0%0/4
not(A) or B and C
A= not defined $rtnB= defined $vC= $v < $rtn
ABCresult
1XX1
0111
00X0
0100
301 }
302 0 return $rtn;
303 }
304 sub max {
305 0 ▶ my $rtn = shift;
Subroutine max: not called CC=5 SCAR=34.0
Pod undocumented
306 0 foreach my $v (@_) {
307 0 ▶ $rtn = $v if !defined $rtn || (defined $v && $v > $rtn);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rtn or defined $v and $v > $rtn 0 0 0
Condition0%0/6
not defined $rtn or defined $v and $v > $rtn
l!l&&r!l&&!r
000
defined $v and $v > $rtn
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $rtn or defined $v and $v > $rtn
not defined $rtn defined $v $v > $rtn
Truth table0%0/4
not(A) or B and C
A= not defined $rtnB= defined $vC= $v > $rtn
ABCresult
1XX1
0111
00X0
0100
308 }
309 0 return $rtn;
310 }
311  
312 34 1;
313 ######################################################################
314 =pod
315  
316 =head1 NAME
317  
318 Parallel::Forker - Parallel job forking and management
319  
320 =head1 SYNOPSIS
321  
322 use Parallel::Forker;
323 $Fork = new Parallel::Forker (use_sig_child=>1);
324 $SIG{CHLD} = sub { Parallel::Forker::sig_child($Fork); };
325 $SIG{TERM} = sub { $Fork->kill_tree_all('TERM') if $Fork && $Fork->in_parent; die "Quitting...\n"; };
326  
327 $Fork->schedule
328 (run_on_start => sub {print "child work here...";},
329 # run_on_start => \&child_subroutine, # Alternative: call a named sub.
330 run_on_finish => sub {print "parent cleanup here...";},
331 )->run;
332  
333 $Fork->wait_all; # Wait for all children to finish
334  
335 # More processes
336 my $p1 = $Fork->schedule(...)->ready;
337 my $p2 = $Fork->schedule(..., run_after=>[$p1])->ready;
338 $Fork->wait_all; # p1 will complete before p2 starts
339  
340 # Other functions
341 $Fork->poll; # Service any active children
342 foreach my $proc ($Fork->running) { # Loop on each running child
343  
344 while ($Fork->is_any_left) {
345 $Fork->poll;
346 usleep(10*1000);
347 }
348  
349 =head1 DESCRIPTION
350  
351 Parallel::Forker manages parallel processes that are either subroutines or
352 system commands. Forker supports most of the features in all the other
353 little packages out there, with the addition of being able to specify
354 complicated expressions to determine which processes run after others, or
355 run when others fail.
356  
357 Function names are loosely based on Parallel::ForkManager.
358  
359 The unique property of Parallel::Forker is the ability to schedule
360 processes based on expressions that are specified when the processes are
361 defined. For example:
362  
363 my $p1 = $Fork->schedule(..., label=>'p1');
364 my $p2 = $Fork->schedule(..., label=>'p2');
365 my $p3 = $Fork->schedule(..., run_after => ["p1 | p2"]);
366 my $p4 = $Fork->schedule(..., run_after => ["p1 & !p2"]);
367  
368 Process p3 is specified to run after process p1 *or* p2 have completed
369 successfully. Process p4 will run after p1 finishes successfully, and
370 process p2 has completed with bad exit status.
371  
372 For more examples, see the tests.
373  
374 =head1 METHODS
375  
376 =over 4
377  
378 =item $self->find_proc_name(<name>)
379  
380 Returns one or more Parallel::Forker::Process objects for the given name (one
381 object returned) or label (one or more objects returned). Returns undef if no
382 processes are found.
383  
384 =item $self->in_parent
385  
386 Return true if and only if called from the parent process (the one that
387 created the Forker object).
388  
389 =item $self->is_any_left
390  
391 Return true if any processes are running, or runnable (need to run).
392  
393 =item $self->kill_all(<signal>)
394  
395 Send a signal to all running children. You probably want to call this only
396 from the parent process that created the Parallel::Forker object, wrap the
397 call in "if ($self->in_parent)."
398  
399 =item $self->kill_tree_all(<signal>)
400  
401 Send a signal to all running children and their subchildren.
402  
403 =item $self->max_proc(<number>)
404  
405 Specify the maximum number of processes that the poll method will run at
406 any one time. Defaults to undef, which runs all possible jobs at once.
407 Max_proc takes effect when you schedule processes and mark them "ready,"
408 then rely on Parallel::Forker's poll method to move the processes from the
409 ready state to the run state. (You should not call ->run yourself, as this
410 starts a new process immediately, ignoring max_proc.)
411  
412 =item $self->new(<parameters>)
413  
414 Create a new manager object. There may be more than one manager in any
415 application, but applications taking advantage of the sig_child handler
416 should call every manager's C<sig_child> method in the application's
417 C<SIGCHLD> handler.
418  
419 Parameters are passed by name as follows:
420  
421 =over 4
422  
423 =item max_proc => (<number>)
424  
425 See the C<max_proc> object method.
426  
427 =item use_sig_child => ( 0 | 1 )
428  
429 See the C<use_sig_child> object method. This option must be specified to
430 prevent a warning.
431  
432 =back
433  
434 =item $self->poll
435  
436 See if any children need work, and service them. Start up to max_proc
437 processes that are "ready" by calling their run method. Non-blocking;
438 always returns immediately.
439  
440 =item $self->process(<process_name>)
441  
442 Return Parallel::Forker::Process object for the specified process name, or
443 undef if none is found. See also find_proc_name.
444  
445 =item $self->processes
446  
447 Return Parallel::Forker::Process objects for all processes.
448  
449 =item $self->processes_sorted
450  
451 Return Parallel::Forker::Process objects for all processes, sorted by name.
452  
453 =item $self->ready_all
454  
455 Mark all processes as ready for scheduling.
456  
457 =item $self->reap_processes
458  
459 Reap all processes which have no other processes waiting for them, and the
460 process is is_done or is_parerr. Returns list of processes reaped. This
461 reclaims memory for when a large number of processes are being created,
462 run, and destroyed.
463  
464 =item $self->running
465  
466 Return Parallel::Forker::Process objects for all processes that are
467 currently running.
468  
469 =item $self->schedule(<parameters>)
470  
471 Register a new process perhaps for later running. Returns a
472 Parallel::Forker::Process object. Parameters are passed by name as
473 follows:
474  
475 =over 4
476  
477 =item label
478  
479 Optional name to use in C<run_after> commands. Unlike C<name>, this may be
480 reused, in which case C<run_after> will wait on all commands with the given
481 label. Labels must contain only [a-zA-Z0-9_].
482  
483 =item name
484  
485 Optional name to use in C<run_after> commands. Note that names MUST be
486 unique! When not specified, a unique number will be assigned
487 automatically.
488  
489 =item run_on_start
490  
491 Subroutine reference to execute when the job begins, in the forked process.
492 The subroutine is called with one argument, a reference to the
493 Parallel::Forker::Process that is starting.
494  
495 If your callback is going to fork, you'd be advised to have the child:
496  
497 $SIG{ALRM} = 'DEFAULT';
498 $SIG{CHLD} = 'DEFAULT';
499  
500 This will prevent the child from inheriting the parent's handlers, and
501 possibly confusing any child calls to waitpid.
502  
503 =item run_on_finish
504  
505 Subroutine reference to execute when the job ends, in the master process.
506 The subroutine is called with two arguments, a reference to the
507 Parallel::Forker::Process that is finishing, and the exit status of the
508 child process. Note the exit status will only be correct if a CHLD signal
509 handler is installed.
510  
511 =item run_after
512  
513 A list reference of processes that must be completed before this process
514 can be runnable. You may pass a process object (from schedule), a process
515 name, or a process label. You may use "|" or "&" in a string to run this
516 process after ANY processes exit, or after ALL exit (the default.)
517 ! in front of a process name indicates to run if that process fails with
518 bad exit status. ^ in front of a process indicates to run if that process
519 succeeds OR fails.
520  
521 =back
522  
523 =item $self->sig_child
524  
525 Must be called in a C<$SIG{CHLD}> handler by the parent process if
526 C<use_sig_child> was called with a "true" value. If there are multiple
527 Parallel::Forker objects each of their C<sig_child> methods must be called
528 in the C<$SIG{CHLD}> handler.
529  
530 =item $self->state_stats
531  
532 Return hash containing statistics with keys of state names, and values with
533 number of processes in each state.
534  
535 =item $self->use_sig_child( 0 | 1 )
536  
537 This should always be called with a 0 or 1. If you install a C<$SIG{CHLD}>
538 handler which calls your Parallel::Forker object's C<sig_child> method, you
539 should also turn on C<use_sig_child>, by calling it with a "true" argument.
540 Then, calls to C<poll()> will do less work when there are no children
541 processes to be reaped. If not using the handler call with 0 to prevent a
542 warning.
543  
544 =item $self->wait_all
545  
546 Wait until there are no running or runable jobs left.
547  
548 =item $self->write_tree(filename => <filename>)
549  
550 Print a dump of the execution tree.
551  
552 =back
553  
554 =head1 DISTRIBUTION
555  
556 The latest version is available from CPAN and from
557 L<https://www.veripool.org/parallel-forker>.
558  
559 Copyright 2002-2020 by Wilson Snyder. This package is free software; you
560 can redistribute it and/or modify it under the terms of either the GNU
561 Lesser General Public License Version 3 or the Perl Artistic License
562 Version 2.0.
563  
564 =head1 AUTHORS
565  
566 Wilson Snyder <wsnyder@wsnyder.org>
567  
568 =head1 SEE ALSO
569  
570 L<Parallel::Forker::Process>
571  
572 =cut
573 ######################################################################
↑ Summary Forker/Process.pm »